Top-rated recipes tagged "meta:loc=24"http://code.activestate.com/recipes/tags/meta:loc=24/top/2017-06-13T10:57:34-07:00ActiveState Code Recipes"Static-methods" (aka "class-methods") in Python (Python)
2001-06-19T10:16:00-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/52304-static-methods-aka-class-methods-in-python/
<p style="color: grey">
Python
recipe 52304
by <a href="/recipes/users/97991/">Alex Martelli</a>
(<a href="/recipes/tags/oop/">oop</a>).
Revision 3.
</p>
<p>An attribute of a class-object is implicitly mutated into an unbound-method object if it starts out as a Python-coded function; thus, such functions must be wrapped as other callables if "just calling them" (without an instance-argument) is desired.</p>
Get names and types of all attributes of a Python module (Python)
2016-10-06T17:21:42-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580705-get-names-and-types-of-all-attributes-of-a-python-/
<p style="color: grey">
Python
recipe 580705
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/documentation/">documentation</a>, <a href="/recipes/tags/introspection/">introspection</a>, <a href="/recipes/tags/modules/">modules</a>, <a href="/recipes/tags/reflection/">reflection</a>, <a href="/recipes/tags/type/">type</a>).
</p>
<p>This recipe shows how to get the names and types of all the attributes of a Python module. This can be useful when exploring new modules (either built-in or third-party), because attributes are mostly a) data elements or b) functions or methods, and for either of those, you would like to know the type of the attribute, so that, if it is a data element, you can print it, and if it is a function or method, you can print its docstring to get brief help on its arguments, processsing and outputs or return values, as a way of learning how to use it.</p>
<p>The code for the recipe includes an example call to it, at the end of the code.
Note that you first have to import the modules that you want to introspect in this way.</p>
Archimedes method for calculating PI (Python)
2009-12-09T15:13:09-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/576981-archimedes-method-for-calculating-pi/
<p style="color: grey">
Python
recipe 576981
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/pi/">pi</a>).
</p>
<p>Archimedes method for calculating PI.</p>
Build a white list of email address from "Sent Items" mailbox. (Python)
2003-04-16T17:48:52-07:00Noah Spurrierhttp://code.activestate.com/recipes/users/103276/http://code.activestate.com/recipes/194372-build-a-white-list-of-email-address-from-sent-item/
<p style="color: grey">
Python
recipe 194372
by <a href="/recipes/users/103276/">Noah Spurrier</a>
(<a href="/recipes/tags/files/">files</a>).
</p>
<p>This extracts all of the To addresses from a file in standard mbox format.
It is used on a "Sent Items" mailbox to build an address white list.
Presumably everyone you send email to is a candidate for an email white list.</p>
spell checking (Python)
2002-03-12T12:59:07-08:00Sébastien Keimhttp://code.activestate.com/recipes/users/131730/http://code.activestate.com/recipes/117221-spell-checking/
<p style="color: grey">
Python
recipe 117221
by <a href="/recipes/users/131730/">Sébastien Keim</a>
(<a href="/recipes/tags/text/">text</a>).
Revision 2.
</p>
<p>use popen2 module to drive the ispell typo checker</p>
Search and replace text in a file. (Python)
2001-03-13T20:18:13-08:00Jeff Bauerhttp://code.activestate.com/recipes/users/98055/http://code.activestate.com/recipes/52250-search-and-replace-text-in-a-file/
<p style="color: grey">
Python
recipe 52250
by <a href="/recipes/users/98055/">Jeff Bauer</a>
(<a href="/recipes/tags/text/">text</a>).
</p>
<p>A simple text search and replace program.</p>
Create Calendars on PDF with a few lines (Python)
2017-06-13T10:57:34-07:00Jorj X. McKiehttp://code.activestate.com/recipes/users/4193772/http://code.activestate.com/recipes/580805-create-calendars-on-pdf-with-a-few-lines/
<p style="color: grey">
Python
recipe 580805
by <a href="/recipes/users/4193772/">Jorj X. McKie</a>
(<a href="/recipes/tags/calendar/">calendar</a>, <a href="/recipes/tags/fitz/">fitz</a>, <a href="/recipes/tags/mupdf/">mupdf</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/pymupdf/">pymupdf</a>).
Revision 2.
</p>
<p>PyMuPDF (fitz) provides easy to use ways to create PDF documents out of simple texts.</p>
<p>An example is the text output of Python's calendar module. Here we take a starting year as script parameter and output a 3-page (A4 landscape) document with calendars for this and the following two years - in less than 20 lines of code.</p>
Mandelbrot Fractal image output to ppm file (Python)
2015-01-17T19:21:47-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/579006-mandelbrot-fractal-image-output-to-ppm-file/
<p style="color: grey">
Python
recipe 579006
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/fractal/">fractal</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/images/">images</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>).
</p>
<p>It creates image output without using any external library.</p>
<p>PPM image files can be viewed and converted to other image file formats using many applications, GIMP or IrfanView for example.</p>
Printing list of ODBC data sources (Python)
2013-12-10T11:07:37-08:00Michal Niklashttp://code.activestate.com/recipes/users/186902/http://code.activestate.com/recipes/578782-printing-list-of-odbc-data-sources/
<p style="color: grey">
Python
recipe 578782
by <a href="/recipes/users/186902/">Michal Niklas</a>
(<a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/odbc/">odbc</a>, <a href="/recipes/tags/sql/">sql</a>).
</p>
<p>This simple code shows ODBC data sources. It uses <code>odbc</code> module.</p>
Windowing an iterable with itertools (Python)
2010-04-15T18:45:41-07:00Daniel Cohnhttp://code.activestate.com/recipes/users/4172918/http://code.activestate.com/recipes/577196-windowing-an-iterable-with-itertools/
<p style="color: grey">
Python
recipe 577196
by <a href="/recipes/users/4172918/">Daniel Cohn</a>
(<a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/iterable/">iterable</a>, <a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/loop/">loop</a>, <a href="/recipes/tags/peeking/">peeking</a>, <a href="/recipes/tags/window/">window</a>).
</p>
<p>Oftentimes a programmer needs to peek into an iterator without advancing it, a task for which many good solutions already exist. But what if the intrepid coder needs a fast and pythonic way to 'window' the data? This recipe demonstrates how to wrap any iterable with a class that adds two methods, prev and peek.</p>
Automatic ref-count management in C++ using a smart ptr (Python)
2007-08-14T15:41:44-07:00Ian Eloffhttp://code.activestate.com/recipes/users/2227021/http://code.activestate.com/recipes/528875-automatic-ref-count-management-in-c-using-a-smart-/
<p style="color: grey">
Python
recipe 528875
by <a href="/recipes/users/2227021/">Ian Eloff</a>
(<a href="/recipes/tags/extending/">extending</a>).
</p>
<p>Managing ref-counting is a complex and error prone business. If you choose C++ to extend or embed Python, you can simply use a modification on std::auto_ptr. Instead of calling delete on the managed pointer, it will decref it.</p>
<p>So now you can do:</p>
<p>auto_py str = PyStr_FromString("Hello World!");</p>
<p>and forget about having to decref it altogether! Just like auto_ptr you can get the PyObject * with get(), release it from managed control with release(), and rebind it with reset(new_ptr). You can also incref it by calling inc(), but be cautious as you can easily create a leak by increfing once to much.</p>
Dice Roll (Python)
2007-07-11T00:28:09-07:00Symon Polleyhttp://code.activestate.com/recipes/users/4056524/http://code.activestate.com/recipes/522978-dice-roll/
<p style="color: grey">
Python
recipe 522978
by <a href="/recipes/users/4056524/">Symon Polley</a>
(<a href="/recipes/tags/oop/">oop</a>).
Revision 5.
</p>
<p>An object oriented approach to rolling a custom dice!
this technique allows a person to define a custom dice by list,
by integer, or by nothing at all defaulting to 6 sides.</p>
Reading from and writing to the Windows Registry (Python)
2001-07-09T12:47:30-07:00Daniel Kinnaerhttp://code.activestate.com/recipes/users/100748/http://code.activestate.com/recipes/66011-reading-from-and-writing-to-the-windows-registry/
<p style="color: grey">
Python
recipe 66011
by <a href="/recipes/users/100748/">Daniel Kinnaer</a>
.
</p>
<p>The code below shows how to read from/write to the Windows Registry. In this example all the tasks are listed which are executed at logon. A new task (opening the explorer) is added to ths logon.</p>
Publish Firebird SQL data to PDF with xtopdf (Python)
2015-12-01T19:45:18-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579131-publish-firebird-sql-data-to-pdf-with-xtopdf/
<p style="color: grey">
Python
recipe 579131
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/firebird/">firebird</a>, <a href="/recipes/tags/pdfwriter/">pdfwriter</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/sql/">sql</a>, <a href="/recipes/tags/xtopdf/">xtopdf</a>).
</p>
<p>This recipe shows how to publish data from the Firebird RDBMS to PDF, using the xtopdf toolkit and the fbd Python driver for Firebird. Firebird is a cross-platform, open source RDBMS based on the former Interbase RDBMS from Borland, which they used to bundle with some of their developmemt tools, such as Borland C++ and Borland Delphi.</p>
<p>The recipe reads data from a Firebird table, using the fbd Python driver for Firebird, and writes it to PDF, using the xtopdf toolkit. See:</p>
<p><a href="http://jugad2.blogspot.in/p/xtopdf-pdf-creation-library.html" rel="nofollow">http://jugad2.blogspot.in/p/xtopdf-pdf-creation-library.html</a></p>
<p>for information on xtopdf.</p>
<p>It assumes that a Firebird database called test.fdb exists under /temp/firebird (C:\temp\firebird, really - the test was done on Windows), and that it has a contacts table with the structure shown in the code of the recipe.</p>
<p>More details and sample output are here:</p>
<p><a href="http://jugad2.blogspot.in/2014/01/by-vasudev-ram-pdf-firebird-is-cross.html" rel="nofollow">http://jugad2.blogspot.in/2014/01/by-vasudev-ram-pdf-firebird-is-cross.html</a></p>
A Bash Beep Command For OSX 10.7+... (Bash)
2014-02-27T19:36:17-08:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578837-a-bash-beep-command-for-osx-107/
<p style="color: grey">
Bash
recipe 578837
by <a href="/recipes/users/4177147/">Barry Walker</a>
(<a href="/recipes/tags/apple/">apple</a>, <a href="/recipes/tags/audio/">audio</a>, <a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/beep/">beep</a>, <a href="/recipes/tags/error_beep/">error_beep</a>, <a href="/recipes/tags/error_sound/">error_sound</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/macbook_pro/">macbook_pro</a>, <a href="/recipes/tags/sound/">sound</a>).
</p>
<p>This small bash script generates an 8044 byte 1KHz sinewave wave file and immediately plays it.
The file created is a _pure_ sinewave and lasts for 1 second. It uses the default "afplay"
command to run the generated file.</p>
<p>It was designed around an Apple Macbook Pro but using "aplay" it might even work on other *nix
flavours from the command line. I have not bothered to try it as this was purely for my MB Pro.</p>
<p>The wave file can be found as "/tmp/sinewave.wav" during the working session(s) and can be saved
anywhere of your choice.</p>
<p>Enjoy...</p>
<p>(Watch for word wrapping etc...)</p>
<p>Bazza.</p>
Simple Text Mode Digital Oscilloscope Graphics DEMO. (Python)
2014-01-21T19:07:49-08:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578812-simple-text-mode-digital-oscilloscope-graphics-dem/
<p style="color: grey">
Python
recipe 578812
by <a href="/recipes/users/4177147/">Barry Walker</a>
(<a href="/recipes/tags/apple/">apple</a>, <a href="/recipes/tags/demo/">demo</a>, <a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/macbook_pro/">macbook_pro</a>).
</p>
<p>This is a DEMO to switch a *NIX terminal from text to _graphics_ mode on the fly.</p>
<p>The graphics characters are limited to lowercase 'a' through 'z' and
this DEMO just prints a small subset to see what a fun, kids level, 8
channel Logic-Analyser/Digital-Oscilloscope might look like.</p>
<p>This may not work in every *NIX style terminal but does work on default
terminals using bash on OSX 10.7.5, PCLinuxOS 2009 and Debian Linux 6.0.x.</p>
<p>Tested on Python 2.5.x to 3.3.2 on the above platforms...
(It might work on earlier versions of Python but untested.)</p>
<p>Enjoy finding simple solutions to often very difficult problems...</p>
<p>Bazza...</p>
Using websocketd with Python for web-based system monitoring (Python)
2014-01-02T22:57:02-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/578803-using-websocketd-with-python-for-web-based-system-/
<p style="color: grey">
Python
recipe 578803
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/go/">go</a>, <a href="/recipes/tags/golang/">golang</a>, <a href="/recipes/tags/psutil/">psutil</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/websockets/">websockets</a>).
</p>
<p>This is the server side of a recipe that shows how the use websocket, a tool written in Go, that sort of daemonizes a program that reads from standard input and writes to standard output, thus making it into a WebSocket server.</p>
CSS line with px value adds a new line with rem (JavaScript)
2013-11-20T21:15:22-08:00Magnus Axelssonhttp://code.activestate.com/recipes/users/4188509/http://code.activestate.com/recipes/578769-css-line-with-px-value-adds-a-new-line-with-rem/
<p style="color: grey">
JavaScript
recipe 578769
by <a href="/recipes/users/4188509/">Magnus Axelsson</a>
(<a href="/recipes/tags/convert/">convert</a>, <a href="/recipes/tags/css/">css</a>, <a href="/recipes/tags/komodo/">komodo</a>, <a href="/recipes/tags/macro/">macro</a>).
</p>
<p>Put your cursor on a CSS line that ends with a value in px (do not select any text). Run the script and it will insert a new line there the px value is converted to a rem value. </p>
<p>You need to edit the code to modify your rem base matching the one you use in your own CSS. The default value is 16.</p>
<p>Example:
font-size: 12px; (run the script with the cursor in this line)
font-size: 0.75rem (new line added by script) </p>
Simple Password Generator (Python)
2012-05-10T16:55:59-07:00Isendrak Skatasmidhttp://code.activestate.com/recipes/users/4182031/http://code.activestate.com/recipes/578125-simple-password-generator/
<p style="color: grey">
Python
recipe 578125
by <a href="/recipes/users/4182031/">Isendrak Skatasmid</a>
(<a href="/recipes/tags/security/">security</a>).
</p>
<p>A simple Password Generator in Python</p>
Round number to specified number of significant digits (Python)
2012-04-26T20:15:48-07:00Ben Hoythttp://code.activestate.com/recipes/users/4170919/http://code.activestate.com/recipes/578114-round-number-to-specified-number-of-significant-di/
<p style="color: grey">
Python
recipe 578114
by <a href="/recipes/users/4170919/">Ben Hoyt</a>
(<a href="/recipes/tags/decimalplaces/">decimalplaces</a>, <a href="/recipes/tags/round/">round</a>, <a href="/recipes/tags/rounding/">rounding</a>, <a href="/recipes/tags/significantfigures/">significantfigures</a>).
</p>
<p>This function rounds num to the specified number of significant digits (rather than decimal places). See doctests for more examples.</p>