Popular recipes tagged "meta:loc=30" but not "algorithms"http://code.activestate.com/recipes/tags/meta:loc=30-algorithms/2017-03-01T17:18:23-08:00ActiveState Code RecipesConvert Microsot Excel (XLSX) to PDF with Python and xtopdf (Python) 2015-11-22T22:15:25-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579128-convert-microsot-excel-xlsx-to-pdf-with-python-and/ <p style="color: grey"> Python recipe 579128 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/excel/">excel</a>, <a href="/recipes/tags/formats/">formats</a>, <a href="/recipes/tags/openpyxl/">openpyxl</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/xlsx/">xlsx</a>, <a href="/recipes/tags/xtopdf/">xtopdf</a>). </p> <p>This recipe shows how the basics of to convert the text data in a Microsoft Excel file (XLSX format) to PDF (Portable Document Format). It uses openpyxl to read the XLSX file and xtopdf to generate the PDF file.</p> Show OS error codes and messages from the os.errno module (Python) 2017-03-01T17:18:23-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580759-show-os-error-codes-and-messages-from-the-oserrno-/ <p style="color: grey"> Python recipe 580759 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/introspection/">introspection</a>, <a href="/recipes/tags/libraries/">libraries</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/unix/">unix</a>, <a href="/recipes/tags/utilities/">utilities</a>, <a href="/recipes/tags/windows/">windows</a>). </p> <p>This recipe is a simple Python introspection utility that displays the defined OS error codes and messages (that Python knows about) from the os.errno module. It works for both Python 2 and Python 3. For each kind of OS error defined in Python, it will display a serial number, the error code, and the corresponding error name, and English error message. E.g. the first few lines of its output are shown below:</p> <p>$ py -2 os_errno_info.py</p> <p>Showing error codes and names</p> <p>from the os.errno module:</p> <p>Python sys.version: 2.7.12</p> <p>Number of error codes: 86</p> <p>Idx Code Name Message</p> <p>0 1 EPERM Operation not permitted</p> <p>1 2 ENOENT No such file or directory</p> <p>2 3 ESRCH No such process</p> <p>3 4 EINTR Interrupted function call</p> <p>4 5 EIO Input/output error</p> <p>More information, full output and other details are available here:</p> <p><a href="https://jugad2.blogspot.in/2017/03/show-error-numbers-and-codes-from.html" rel="nofollow">https://jugad2.blogspot.in/2017/03/show-error-numbers-and-codes-from.html</a></p> Easily create a Python REPL in Python (Python) 2016-10-31T21:53:30-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580712-easily-create-a-python-repl-in-python/ <p style="color: grey"> Python recipe 580712 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/code_module/">code_module</a>, <a href="/recipes/tags/dynamic/">dynamic</a>, <a href="/recipes/tags/evaluation/">evaluation</a>, <a href="/recipes/tags/read_eval_print_loop/">read_eval_print_loop</a>, <a href="/recipes/tags/repl/">repl</a>). </p> <p>This recipe shows how to easily create a Python REPL (Read-Eval-Print Loop) in Python itself. This can allow the user to interact with a running Python program, including typing in Python statements at the REPL prompt, defining functions, using and changing variables that were set before the interaction started, and those variables modified during the interaction, will persist in the memory of the program, for any use, even after the interaction is over, as long as the program continues to run.</p> Simulate C's switch statement (Python) 2016-12-11T16:28:50-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580730-simulate-cs-switch-statement/ <p style="color: grey"> Python recipe 580730 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/c/">c</a>, <a href="/recipes/tags/features/">features</a>, <a href="/recipes/tags/language/">language</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/switch/">switch</a>). </p> <p>This recipe shows a Python construct that can behave somewhat like C's switch statement. It is not a perfect one-to-one simulation. But it does have some of the features of the C switch. One feature not supported is the fall-through feature in C's switch.</p> Decrypt a PDF using fitz / MuPDF (PyMuPDF) (Python) 2016-03-17T12:22:10-07:00Harald Liederhttp://code.activestate.com/recipes/users/4191581/http://code.activestate.com/recipes/580627-decrypt-a-pdf-using-fitz-mupdf-pymupdf/ <p style="color: grey"> Python recipe 580627 by <a href="/recipes/users/4191581/">Harald Lieder</a> (<a href="/recipes/tags/decompression/">decompression</a>, <a href="/recipes/tags/decryption/">decryption</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/repair/">repair</a>). </p> <p>It's more a code snippet. Shows how to dynamically check whether a PDF is password protected. If it is, decrypt it and save it back to disk un-encrypted.</p> Saving a numpy array to png image using fitz / MuPDF (PyMuPDF) (Python) 2016-03-19T12:57:38-07:00Harald Liederhttp://code.activestate.com/recipes/users/4191581/http://code.activestate.com/recipes/580630-saving-a-numpy-array-to-png-image-using-fitz-mupdf/ <p style="color: grey"> Python recipe 580630 by <a href="/recipes/users/4191581/">Harald Lieder</a> (<a href="/recipes/tags/fitz/">fitz</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/mupdf/">mupdf</a>, <a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/png/">png</a>, <a href="/recipes/tags/pymupdf/">pymupdf</a>). Revision 2. </p> <p>If a pixel array is defined in numpy, it can be saved as a PNG image VERY fast if you use PyMuPDF.</p> Drive which Python functions are executed via a text file (Python) 2016-06-12T15:27:59-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580681-drive-which-python-functions-are-executed-via-a-te/ <p style="color: grey"> Python recipe 580681 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/adapter/">adapter</a>, <a href="/recipes/tags/configuration/">configuration</a>, <a href="/recipes/tags/dynamic/">dynamic</a>, <a href="/recipes/tags/dynamic_method/">dynamic_method</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/runtime/">runtime</a>). </p> <p>This recipe shows a simple way of externally controlling which Python functions (out out of some) in your program get executed. The control is done at run time, so no code changes to the program are needed, for different choices. The control is done using a text file.</p> Multiple host/ip DNS resolution - mass_nslookup.pl (Perl) 2014-07-31T17:11:03-07:00Brett Carrollhttp://code.activestate.com/recipes/users/4174322/http://code.activestate.com/recipes/578915-multiple-hostip-dns-resolution-mass_nslookuppl/ <p style="color: grey"> Perl recipe 578915 by <a href="/recipes/users/4174322/">Brett Carroll</a> (<a href="/recipes/tags/address/">address</a>, <a href="/recipes/tags/digg/">digg</a>, <a href="/recipes/tags/dns/">dns</a>, <a href="/recipes/tags/hostname/">hostname</a>, <a href="/recipes/tags/ip/">ip</a>, <a href="/recipes/tags/nslookup/">nslookup</a>, <a href="/recipes/tags/perl/">perl</a>). </p> <p>Parse a text file containing a list of IP Addresses or hostnames (one per line) and print out the corresponding IP Address or hostname resolved by DNS.</p> Decorator to check method param types (Python) 2014-01-14T11:23:40-08:00Andrey Nikishaevhttp://code.activestate.com/recipes/users/4176176/http://code.activestate.com/recipes/578809-decorator-to-check-method-param-types/ <p style="color: grey"> Python recipe 578809 by <a href="/recipes/users/4176176/">Andrey Nikishaev</a> (<a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/method/">method</a>, <a href="/recipes/tags/typecheck/">typecheck</a>). </p> <p>This solution give possibility to check method param type, raise needed exception type, and also have good readability in the decorator definition.</p> Design by contract on python vanilla (Python) 2013-11-05T20:50:30-08:00Alan Cristhian Ruizhttp://code.activestate.com/recipes/users/4186199/http://code.activestate.com/recipes/578767-design-by-contract-on-python-vanilla/ <p style="color: grey"> Python recipe 578767 by <a href="/recipes/users/4186199/">Alan Cristhian Ruiz</a> (<a href="/recipes/tags/demo/">demo</a>, <a href="/recipes/tags/design_pattern/">design_pattern</a>, <a href="/recipes/tags/productivity/">productivity</a>). Revision 2. </p> <p>The code below is an example that shows that we can do design by contract in python without any non-standard module.</p> Search engine (Batch) 2013-10-10T16:18:09-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578646-search-engine/ <p style="color: grey"> Batch recipe 578646 by <a href="/recipes/users/4184115/">greg zakharov</a> (<a href="/recipes/tags/search/">search</a>). Revision 2. </p> <p>Quick search on files index embedded directly in the command script. For example: "search.cmd exe$" - looks for all EXE files in index or just "search.cmd blahblahblah" - looks for all matches with "blahblahblah" in paths. To remove index from script use "search.cmd /fix:me" command (without quotes).</p> 3D polygon area (Python) 2012-10-01T16:29:26-07:00Jamie Bullhttp://code.activestate.com/recipes/users/4183059/http://code.activestate.com/recipes/578276-3d-polygon-area/ <p style="color: grey"> Python recipe 578276 by <a href="/recipes/users/4183059/">Jamie Bull</a> (<a href="/recipes/tags/3d/">3d</a>, <a href="/recipes/tags/area/">area</a>, <a href="/recipes/tags/geometry/">geometry</a>, <a href="/recipes/tags/polygon/">polygon</a>). </p> <p>Function for finding the area of a polygon in a 3D co-ordinate system.</p> Basic threaded Python TCP server (Python) 2012-09-19T17:59:01-07:00Luis Martin Gilhttp://code.activestate.com/recipes/users/4183220/http://code.activestate.com/recipes/578247-basic-threaded-python-tcp-server/ <p style="color: grey"> Python recipe 578247 by <a href="/recipes/users/4183220/">Luis Martin Gil</a> (<a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/server/">server</a>, <a href="/recipes/tags/tcp/">tcp</a>, <a href="/recipes/tags/thread/">thread</a>). Revision 3. </p> <p>TCP provides reliable, ordered delivery of a stream of bytes from a program on one computer to another program on another computer. Sometimes you need a quick deployment of a TCP server and here I bring to you a Python 2.* snippet of a threaded Python server.</p> Command Line Windows Alarm Clock (Python) 2012-12-05T23:53:49-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578362-command-line-windows-alarm-clock/ <p style="color: grey"> Python recipe 578362 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/alarm/">alarm</a>, <a href="/recipes/tags/clock/">clock</a>, <a href="/recipes/tags/demonstration/">demonstration</a>, <a href="/recipes/tags/windows/">windows</a>). </p> <p>Using primitive but simple calculations, the alarm program below will find the offset to the time specified, sleep, and awake to run an alarm until terminated. This Windows recipe utilizes the <code>msvcrt</code> and <code>winsound</code> modules to operate and has limited use elsewhere.</p> Executing VBScript without creation of it (Batch) 2013-10-10T16:20:45-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578329-executing-vbscript-without-creation-of-it/ <p style="color: grey"> Batch recipe 578329 by <a href="/recipes/users/4184115/">greg zakharov</a> (<a href="/recipes/tags/cscript/">cscript</a>). Revision 2. </p> <p>Have you a wish to launch VBScript without temporary files and right from batch? Maybe you think that it's not possible, but you think wrong. Do you remember my <a href="http://code.activestate.com/recipes/578315-create-cab-or-zip-with-batch/?in=lang-batch">post</a> for executing JScript from batch? So, maybe you are familiar with 'MSScriptControl' object which helps to execute JScript code in VBScripts or conversely - VBScript code inside JScripts. Let's take a look.</p> Function that supports dividing timedelta by float (Python) 2012-05-17T13:47:07-07:00Ben Hoythttp://code.activestate.com/recipes/users/4170919/http://code.activestate.com/recipes/578136-function-that-supports-dividing-timedelta-by-float/ <p style="color: grey"> Python recipe 578136 by <a href="/recipes/users/4170919/">Ben Hoyt</a> (<a href="/recipes/tags/datetime/">datetime</a>, <a href="/recipes/tags/division/">division</a>, <a href="/recipes/tags/float/">float</a>). </p> <p>Python 2.x doesn't support dividing a timedelta by a float (only works with int). This function adds support for that.</p> Dynamic function parameters using annotations (Python) 2012-02-22T22:31:02-08:00pavelhttp://code.activestate.com/recipes/users/4171837/http://code.activestate.com/recipes/578049-dynamic-function-parameters-using-annotations/ <p style="color: grey"> Python recipe 578049 by <a href="/recipes/users/4171837/">pavel</a> (<a href="/recipes/tags/annotations/">annotations</a>, <a href="/recipes/tags/decorator/">decorator</a>). </p> <p>Python default values for keyword arguments are evaluated only once and not on every function call. For example following function will not work as user may expect:</p> <pre class="prettyprint"><code>def printNow(l=[], now=datetime.now()): l.append(len(l)) print('List:', l, ' id:', id(l)) print('Now:', now) for i in range(3): printNow() print() </code></pre> <p>The "dynamic" decorator solves problem by evaluating callables, that are assigned to parameters using annotations syntax (see PEP 3107).</p> Tetration Fractal (Python) 2011-10-24T02:55:08-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577917-tetration-fractal/ <p style="color: grey"> Python recipe 577917 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/fractal/">fractal</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>). </p> <p>Tetration Fractal.</p> Blocks for python (Python) 2011-10-24T22:26:07-07:00yoav glaznerhttp://code.activestate.com/recipes/users/4178625/http://code.activestate.com/recipes/577920-blocks-for-python/ <p style="color: grey"> Python recipe 577920 by <a href="/recipes/users/4178625/">yoav glazner</a> (<a href="/recipes/tags/block/">block</a>, <a href="/recipes/tags/lambda/">lambda</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/style/">style</a>). </p> <p>A simple <strong>block</strong> function that lets one send multi line blocks of code to a function it doesn't really act like a <em>normal</em> def/lambda but offers cool style</p> Iterator Offsetter (Python) 2012-04-16T13:08:55-07:00Josh Bodehttp://code.activestate.com/recipes/users/4179046/http://code.activestate.com/recipes/577852-iterator-offsetter/ <p style="color: grey"> Python recipe 577852 by <a href="/recipes/users/4179046/">Josh Bode</a> (<a href="/recipes/tags/iterable/">iterable</a>, <a href="/recipes/tags/itertools/">itertools</a>, <a href="/recipes/tags/offset/">offset</a>). Revision 2. </p> <p>Produces a list of copies of an iterable that are offset by the supplied offsets.</p>