Popular recipes tagged "meta:loc=32"http://code.activestate.com/recipes/tags/meta:loc=32/2017-04-27T21:26:00-07:00ActiveState Code RecipesClassifying characters using nested conditional expressions (Python)
2017-04-27T21:26:00-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580792-classifying-characters-using-nested-conditional-ex/
<p style="color: grey">
Python
recipe 580792
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/characters/">characters</a>, <a href="/recipes/tags/classification/">classification</a>, <a href="/recipes/tags/conditional_expressions/">conditional_expressions</a>, <a href="/recipes/tags/expressions/">expressions</a>, <a href="/recipes/tags/join/">join</a>, <a href="/recipes/tags/lambda/">lambda</a>, <a href="/recipes/tags/map/">map</a>).
</p>
<p>Python has a feature called conditional expressions, similar to C's ternary operator. For example:</p>
<p>print n, 'is odd' if n % 2 == 1 else 'is even'</p>
<p>Here, the conditional expression is this part of the print statement above:</p>
<p>'is odd' if n % 2 == 1 else 'is even'</p>
<p>This expression evaluates to 'is odd' if the condition after the if keyword is True, and evaluates to 'is even' otherwise.</p>
<p>The Python Language Reference section for conditional expressions shows that they can be nested. This recipe shows that we can use nested conditional expressions (within a return statement in a user-defined function) to classify characters into lowercase letters, uppercase letters, or neither.</p>
<p>It also shows how to do the same task using map, lambda and string.join, again with a nested conditional expression, but without using return or a user-defined function.</p>
Trap KeyboardInterrupt and EOFError for graceful program termination (Python)
2016-11-13T20:17:03-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580718-trap-keyboardinterrupt-and-eoferror-for-graceful-p/
<p style="color: grey">
Python
recipe 580718
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/ascii/">ascii</a>, <a href="/recipes/tags/error/">error</a>, <a href="/recipes/tags/exception/">exception</a>, <a href="/recipes/tags/handler/">handler</a>, <a href="/recipes/tags/shutdown/">shutdown</a>, <a href="/recipes/tags/terminate/">terminate</a>, <a href="/recipes/tags/utilities/">utilities</a>, <a href="/recipes/tags/utility/">utility</a>).
</p>
<p>This recipe shows how to trap the KeyboardInterrupt and EOFError Python exceptions so that they do not crash your program. As a vehicle to show this, it uses a small Python utility that shows the ASCII code for any ASCII character you type.</p>
Send autohotkey commands with Python (Python)
2017-01-02T01:33:31-08:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580740-send-autohotkey-commands-with-python/
<p style="color: grey">
Python
recipe 580740
by <a href="/recipes/users/4189907/">Miguel Martínez López</a>
(<a href="/recipes/tags/autohotkey/">autohotkey</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>I use an asterisk (*) as a command line argument to read the autohotkey script from standard input (stdin).
<a href="https://autohotkey.com/docs/Scripts.htm" rel="nofollow">https://autohotkey.com/docs/Scripts.htm</a></p>
Convert HTML to PDF with the PDFcrowd API (Python)
2015-03-07T20:22:54-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579032-convert-html-to-pdf-with-the-pdfcrowd-api/
<p style="color: grey">
Python
recipe 579032
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/api/">api</a>, <a href="/recipes/tags/html/">html</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/pdfcrowd/">pdfcrowd</a>).
</p>
<p>This recipe shows how to use Python and the PDFcrowd API to convert HTML content to PDF. The HTML input can come from a remote URL, a local HTML file, or a string containing HTML.</p>
Serve PDF with Netius, a pure-Python network library, and xtopdf (Python)
2014-12-03T21:27:54-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/578974-serve-pdf-with-netius-a-pure-python-network-librar/
<p style="color: grey">
Python
recipe 578974
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/client/">client</a>, <a href="/recipes/tags/client_server/">client_server</a>, <a href="/recipes/tags/networking/">networking</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/server/">server</a>).
</p>
<p>This recipe shows how to serve PDF from a server written using Netius, a pure-Python library, together with xtopdf, a Python toolkit for PDF creation. It is a proof-of-concept recipe, to show the essentials needed for the task, so it hard-codes the text content that is served as PDF, but the concepts shown can easily be extended to serve dynamically generated PDF content.</p>
division digit-by digit calculation (Python)
2014-09-19T08:33:35-07:00juanhttp://code.activestate.com/recipes/users/4190606/http://code.activestate.com/recipes/578939-division-digit-by-digit-calculation/
<p style="color: grey">
Python
recipe 578939
by <a href="/recipes/users/4190606/">juan</a>
.
</p>
<p>Calculate a division digit by digit with t decimals</p>
Decorator to check if needed modules for method are imported (Python)
2014-03-18T14:11:20-07:00Andrey Nikishaevhttp://code.activestate.com/recipes/users/4176176/http://code.activestate.com/recipes/578852-decorator-to-check-if-needed-modules-for-method-ar/
<p style="color: grey">
Python
recipe 578852
by <a href="/recipes/users/4176176/">Andrey Nikishaev</a>
(<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/import/">import</a>).
</p>
<p>Check if needed modules imported before run method</p>
<pre class="prettyprint"><code>Example::
@require_module(['time'],exception=Exception)
def get_time():
return time.time()
</code></pre>
Extended Euclidean Algorithm (Python)
2016-03-03T07:12:28-08:00Samuel James Ericksonhttp://code.activestate.com/recipes/users/4187478/http://code.activestate.com/recipes/578631-extended-euclidean-algorithm/
<p style="color: grey">
Python
recipe 578631
by <a href="/recipes/users/4187478/">Samuel James Erickson</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/common/">common</a>, <a href="/recipes/tags/discrete/">discrete</a>, <a href="/recipes/tags/divisor/">divisor</a>, <a href="/recipes/tags/euclid/">euclid</a>, <a href="/recipes/tags/extended/">extended</a>, <a href="/recipes/tags/gcd/">gcd</a>, <a href="/recipes/tags/greatest/">greatest</a>, <a href="/recipes/tags/logarithm/">logarithm</a>).
Revision 2.
</p>
<p>given input of integers a and b, this program returns GCD(a,b) along with integers x and y such that ax+by=GCD(a,b).</p>
Generates tuples of integers with a given sum (Python)
2013-07-16T14:03:08-07:00SnarkTurnehttp://code.activestate.com/recipes/users/4185559/http://code.activestate.com/recipes/578608-generates-tuples-of-integers-with-a-given-sum/
<p style="color: grey">
Python
recipe 578608
by <a href="/recipes/users/4185559/">SnarkTurne</a>
.
Revision 3.
</p>
<p>This is a generator that generates all tuples of n integers (>=0) with a given sum s.
The generator can make the difference between tuples (3,0,0) and (0,3,0) or not (see order parameter). </p>
Complex Methods to Compute Pi in Python (Python)
2013-06-03T18:08:19-07:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578548-complex-methods-to-compute-pi-in-python/
<p style="color: grey">
Python
recipe 578548
by <a href="/recipes/users/4184772/">Captain DeadBones</a>
(<a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/pi/">pi</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>This script was written for an article I wrote about computing Pi with Python. This module uses the Decimal library. For more information <a href="http://thelivingpearl.com/2013/05/28/computing-pi-with-python/">How to Compute Pi in Python</a></p>
Checking hashes of files (Ruby)
2012-11-19T08:45:51-08:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578339-checking-hashes-of-files/
<p style="color: grey">
Ruby
recipe 578339
by <a href="/recipes/users/4184115/">greg zakharov</a>
(<a href="/recipes/tags/hash/">hash</a>, <a href="/recipes/tags/ironruby/">ironruby</a>, <a href="/recipes/tags/md5/">md5</a>, <a href="/recipes/tags/ripemd160/">ripemd160</a>, <a href="/recipes/tags/sha1/">sha1</a>, <a href="/recipes/tags/sha256/">sha256</a>, <a href="/recipes/tags/sha384/">sha384</a>, <a href="/recipes/tags/sha512/">sha512</a>).
</p>
<p>There is HashAlgorithm class in System.Security.Cryptography namespace which is stored into mscorlib, a core assembly of .NET Framework. So...</p>
Simple Dijkstra Algorithm (Python)
2012-06-08T02:20:18-07:00Filippo Squillacehttp://code.activestate.com/recipes/users/4174931/http://code.activestate.com/recipes/578156-simple-dijkstra-algorithm/
<p style="color: grey">
Python
recipe 578156
by <a href="/recipes/users/4174931/">Filippo Squillace</a>
(<a href="/recipes/tags/graph/">graph</a>).
Revision 3.
</p>
<p>The algorithm uses the priority queue version of Dijkstra and return the distance between the source node and the others nodes d(s,i). </p>
Komodo JS Macro -- Escape HTML Special Characters in current file (JavaScript)
2012-05-25T20:04:49-07:00Keegan Brownhttp://code.activestate.com/recipes/users/4182206/http://code.activestate.com/recipes/578146-komodo-js-macro-escape-html-special-characters-in-/
<p style="color: grey">
JavaScript
recipe 578146
by <a href="/recipes/users/4182206/">Keegan Brown</a>
(<a href="/recipes/tags/characters/">characters</a>, <a href="/recipes/tags/html/">html</a>, <a href="/recipes/tags/macro/">macro</a>, <a href="/recipes/tags/special/">special</a>, <a href="/recipes/tags/xhtml/">xhtml</a>).
</p>
<p>Escapes all Special Characters to their HTML Special Character equivalent.</p>
Fast Indexing functions (greater than, less than, equal to, and not equal to) (Python)
2012-03-13T16:21:36-07:00Garretthttp://code.activestate.com/recipes/users/4181290/http://code.activestate.com/recipes/578071-fast-indexing-functions-greater-than-less-than-equ/
<p style="color: grey">
Python
recipe 578071
by <a href="/recipes/users/4181290/">Garrett</a>
(<a href="/recipes/tags/index/">index</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/list_comprehension/">list_comprehension</a>, <a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/python/">python</a>).
Revision 2.
</p>
<p>Oftentimes you want to find the index of a list-like object. Numpy arrays, for example, do not have a index member function. These get the job done quickly.</p>
<p>Note: these do not raise exceptions, instead they return -1 on error. You should change that if you want different behavior.</p>
Left-handed password generator (Python)
2011-10-31T12:51:14-07:00Alexhttp://code.activestate.com/recipes/users/4179771/http://code.activestate.com/recipes/577930-left-handed-password-generator/
<p style="color: grey">
Python
recipe 577930
by <a href="/recipes/users/4179771/">Alex</a>
(<a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/lefthand/">lefthand</a>, <a href="/recipes/tags/lefthanded/">lefthanded</a>, <a href="/recipes/tags/password/">password</a>, <a href="/recipes/tags/passwords/">passwords</a>).
Revision 2.
</p>
<p>Generate passwords that can easily be typed with only the left hand, very simple script</p>
Check PC Power (Python)
2011-08-28T12:25:49-07:00Frank Larkinhttp://code.activestate.com/recipes/users/4179117/http://code.activestate.com/recipes/577861-check-pc-power/
<p style="color: grey">
Python
recipe 577861
by <a href="/recipes/users/4179117/">Frank Larkin</a>
(<a href="/recipes/tags/api/">api</a>, <a href="/recipes/tags/computer/">computer</a>, <a href="/recipes/tags/passing/">passing</a>, <a href="/recipes/tags/pointers/">pointers</a>, <a href="/recipes/tags/power/">power</a>, <a href="/recipes/tags/widows/">widows</a>).
</p>
<p>Call windows API GetSystemPowerStatus to determine if the AC power is on or off. Great example of calling Windows APIs using Ptyhon.</p>
TCP Port Checker (Python)
2011-06-24T09:53:28-07:00Boubakrhttp://code.activestate.com/recipes/users/4176416/http://code.activestate.com/recipes/577769-tcp-port-checker/
<p style="color: grey">
Python
recipe 577769
by <a href="/recipes/users/4176416/">Boubakr</a>
(<a href="/recipes/tags/checker/">checker</a>, <a href="/recipes/tags/port/">port</a>, <a href="/recipes/tags/tcp/">tcp</a>).
</p>
<p>a simple TCP Port Checker using socket !</p>
Abstract method decorator (Python)
2011-04-20T21:50:23-07:00jimmy2timeshttp://code.activestate.com/recipes/users/4177690/http://code.activestate.com/recipes/577666-abstract-method-decorator/
<p style="color: grey">
Python
recipe 577666
by <a href="/recipes/users/4177690/">jimmy2times</a>
(<a href="/recipes/tags/abstract/">abstract</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/exception/">exception</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/method/">method</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/subclass/">subclass</a>).
Revision 5.
</p>
<p>A simple decorator that helps define abstract methods: when such a method is called, an appropriate exception is raised.</p>
dualmethod descriptor (Python)
2010-02-06T20:54:45-08:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577030-dualmethod-descriptor/
<p style="color: grey">
Python
recipe 577030
by <a href="/recipes/users/4172944/">Steven D'Aprano</a>
(<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/descriptor/">descriptor</a>, <a href="/recipes/tags/method/">method</a>).
Revision 3.
</p>
<p>This descriptor can be used to decorate methods, similar to the built-ins classmethod and staticmethod. It enables the caller to call methods on either the class or an instance, and the first argument passed to the method will be the class or the instance respectively.</p>
<p>This differs from classmethods, which always passes the class, and staticmethods, which don't pass either.</p>
<p>Like all descriptors, you can only use this in new-style classes.</p>
Find the oldest (or yougest) of a list of files (Python)
2009-06-10T16:08:13-07:00Micah Elliotthttp://code.activestate.com/recipes/users/2649403/http://code.activestate.com/recipes/576804-find-the-oldest-or-yougest-of-a-list-of-files/
<p style="color: grey">
Python
recipe 576804
by <a href="/recipes/users/2649403/">Micah Elliott</a>
(<a href="/recipes/tags/age/">age</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/find/">find</a>, <a href="/recipes/tags/queue/">queue</a>).
</p>
<p>Sometimes you need to perform an operation on the oldest of a set of files. Using <em>get_oldest_file</em> you could implement an age-based priority queue that processes files from oldest to newest. The list of files you pass in may be from a <em>glob</em> of a single directory or some more elaborate search routine.</p>