Top-rated recipes tagged "meta:loc=30"http://code.activestate.com/recipes/tags/meta:loc=30/top/2017-03-01T17:18:23-08:00ActiveState Code RecipesGroupby (Python)
2004-02-12T07:05:02-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/259173-groupby/
<p style="color: grey">
Python
recipe 259173
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 5.
</p>
<p>Guido inspired SQL-like GROUPBY class that also encapsulates the logic in a Unix-like "sort | uniq".</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>
Logging to console .. without surprises (Python)
2009-06-25T13:12:55-07:00Sridhar Ratnakumarhttp://code.activestate.com/recipes/users/4169511/http://code.activestate.com/recipes/576819-logging-to-console-without-surprises/
<p style="color: grey">
Python
recipe 576819
by <a href="/recipes/users/4169511/">Sridhar Ratnakumar</a>
(<a href="/recipes/tags/logging/">logging</a>).
Revision 3.
</p>
<p><code>logging.StreamHandler</code> is not the best way to log to console .. as it prints everything to <code>sys.stderr</code> by default. You can configure it to log to <code>sys.stdout</code> .. but that means even error/exception will be printed to <code>sys.stdout</code>.</p>
Summary reports using itertools.groupby (Python)
2004-09-10T13:20:37-07:00Paul Moorehttp://code.activestate.com/recipes/users/1826654/http://code.activestate.com/recipes/304162-summary-reports-using-itertoolsgroupby/
<p style="color: grey">
Python
recipe 304162
by <a href="/recipes/users/1826654/">Paul Moore</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 2.
</p>
<p>Lists of data grouped by a key value are common - obvious examples are spreadsheets or other tabular arrangements of information. In many cases, the new itertools groupby function introduced in Python 2.4 can provide a means of easily generating summaries of such information.</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>
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>
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>
Error logging with context manager and decorator (Python)
2007-09-19T06:52:58-07:00Kent Johnsonhttp://code.activestate.com/recipes/users/2016182/http://code.activestate.com/recipes/531821-error-logging-with-context-manager-and-decorator/
<p style="color: grey">
Python
recipe 531821
by <a href="/recipes/users/2016182/">Kent Johnson</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
</p>
<p>A context manager and decorator that wrap common exception trapping and handling code so they may be applied with just a single line of code in the client.</p>
Run a task every few seconds (Python)
2001-06-18T04:31:57-07:00Itamar Shtull-Trauringhttp://code.activestate.com/recipes/users/98053/http://code.activestate.com/recipes/65222-run-a-task-every-few-seconds/
<p style="color: grey">
Python
recipe 65222
by <a href="/recipes/users/98053/">Itamar Shtull-Trauring</a>
(<a href="/recipes/tags/threads/">threads</a>).
</p>
<p>The TaskThread class allows you to create threads that execute a specific action at a specified interval, by subclassing - just override the task() method. You can also shutdown a TaskThread without having to wait for it to finish "sleeping" (due to the use of threading.Event objects as an alternative to time.sleep()).</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>
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>
Convert 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>
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>
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>
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>
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>
8 queens (Python)
2010-09-30T15:35:12-07:00mihai miulescuhttp://code.activestate.com/recipes/users/4175122/http://code.activestate.com/recipes/577414-8-queens/
<p style="color: grey">
Python
recipe 577414
by <a href="/recipes/users/4175122/">mihai miulescu</a>
.
</p>
<p>classical backtracking algorithm</p>
Keyword Argument Injection with Python Decorators (Python)
2010-09-05T17:06:04-07:00Ahmet Emre Aladağhttp://code.activestate.com/recipes/users/4174877/http://code.activestate.com/recipes/577382-keyword-argument-injection-with-python-decorators/
<p style="color: grey">
Python
recipe 577382
by <a href="/recipes/users/4174877/">Ahmet Emre Aladağ</a>
(<a href="/recipes/tags/class/">class</a>, <a href="/recipes/tags/class_decorator/">class_decorator</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/injection/">injection</a>, <a href="/recipes/tags/variable/">variable</a>).
Revision 2.
</p>
<p>In most of the object oriented codes we write, we need to set class attributes to the given argument values and this is a very line-consuming thing. To get over these redundant lines, I found a solution using decorators. </p>
custom django login_required decorator (Python)
2006-10-24T08:54:07-07:00Jian Ding Chenhttp://code.activestate.com/recipes/users/2927458/http://code.activestate.com/recipes/498217-custom-django-login_required-decorator/
<p style="color: grey">
Python
recipe 498217
by <a href="/recipes/users/2927458/">Jian Ding Chen</a>
(<a href="/recipes/tags/web/">web</a>).
Revision 2.
</p>
<p>I found django's auth module is too complex for my needs, instead I wrote a relative simple one, but I miss the login_required decorator in django auth module, so I decide to write a new one.</p>
Pass Javascript arrays to PHP (PHP)
2005-05-16T11:45:33-07:00Noah Spurrierhttp://code.activestate.com/recipes/users/103276/http://code.activestate.com/recipes/414334-pass-javascript-arrays-to-php/
<p style="color: grey">
PHP
recipe 414334
by <a href="/recipes/users/103276/">Noah Spurrier</a>
(<a href="/recipes/tags/session/">session</a>).
</p>
<p>This is a Javascript function that will convert a Javascript array to a string in PHP serialized format. You can pass this string to a PHP script and easily unserialize it to a PHP array.</p>