Most viewed recipes tagged "meta:loc=24"http://code.activestate.com/recipes/tags/meta:loc=24/views/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> 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> Random selection of elements in a list, with no repeats (Python) 2002-06-05T05:50:10-07:00Iuri Wickerthttp://code.activestate.com/recipes/users/111694/http://code.activestate.com/recipes/59883-random-selection-of-elements-in-a-list-with-no-rep/ <p style="color: grey"> Python recipe 59883 by <a href="/recipes/users/111694/">Iuri Wickert</a> (<a href="/recipes/tags/search/">search</a>). Revision 2. </p> <p>The simplest, direct way of consuming a list in a random fashion is painfully slow for list with a few 100's of elements. There are equally simple, but much faster ways to do this.</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> Add a method to a class instance at runtime (Python) 2001-02-22T17:04:07-08:00asdfsa asdfdsafhttp://code.activestate.com/recipes/users/515672/http://code.activestate.com/recipes/52192-add-a-method-to-a-class-instance-at-runtime/ <p style="color: grey"> Python recipe 52192 by <a href="/recipes/users/515672/">asdfsa asdfdsaf</a> (<a href="/recipes/tags/debugging/">debugging</a>). </p> <p>This recipe demonstrates the runtime addition of a __str__ method to a class instance. This can sometimes be useful for debugging purposes. It also demonstrates the use of the two special attributes of class instances: '__dict__' and '__class__'.</p> Format integer as binary string (Python) 2003-09-02T12:25:15-07:00Antti Kaiholahttp://code.activestate.com/recipes/users/312792/http://code.activestate.com/recipes/219300-format-integer-as-binary-string/ <p style="color: grey"> Python recipe 219300 by <a href="/recipes/users/312792/">Antti Kaihola</a> (<a href="/recipes/tags/text/">text</a>). </p> <p>These one-liner lambdas convert integers to binary strings with no leading zeros.</p> Recursively print (nested) dictionaries (Python) 2012-04-04T15:20:42-07:00Mauricio Dada Fonseca de Freitashttp://code.activestate.com/recipes/users/4179701/http://code.activestate.com/recipes/578094-recursively-print-nested-dictionaries/ <p style="color: grey"> Python recipe 578094 by <a href="/recipes/users/4179701/">Mauricio Dada Fonseca de Freitas</a> (<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/print/">print</a>, <a href="/recipes/tags/recursion/">recursion</a>). </p> <p>Snippet of code that uses recursion to print nested dictionaries. It does not sort the entries!</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> Implementing a circular data structure using lists (Python) 2001-03-13T18:16:59-08:00Chris McDonoughhttp://code.activestate.com/recipes/users/98018/http://code.activestate.com/recipes/52246-implementing-a-circular-data-structure-using-lists/ <p style="color: grey"> Python recipe 52246 by <a href="/recipes/users/98018/">Chris McDonough</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>In some applications, it's advantageous to be able to define a circular data structure (a structure in which the last element is a pointer to the first). Python lists make it easy to make such a beast. A list is an ordered sequence of elements, so implementing a "ring" as a Python list is easy. "Turning" the ring consists of removing the last element from the list and placing it in the list's beginning slot. We can encapsulate this behavior in a class, which is shown below as the "Ring" class.</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> Newton's Method to Solve Equations in Python (Python) 2013-01-16T16:40:55-08:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578419-newtons-method-to-solve-equations-in-python/ <p style="color: grey"> Python recipe 578419 by <a href="/recipes/users/4184772/">Captain DeadBones</a> (<a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/newtons_method/">newtons_method</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>This is how you would use Newton's method to solve equations. For more information about solving equations in python checkout <a href="http://thelivingpearl.com/2013/01/15/the-easy-way-to-solve-equations-in-python/">How to solve equations using python</a>.</p> Pretty Print table in tabular format (Python) 2013-12-30T23:05:55-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/578801-pretty-print-table-in-tabular-format/ <p style="color: grey"> Python recipe 578801 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/pretty_printer/">pretty_printer</a>, <a href="/recipes/tags/print/">print</a>, <a href="/recipes/tags/printer/">printer</a>, <a href="/recipes/tags/printf/">printf</a>, <a href="/recipes/tags/printing/">printing</a>). </p> <p>A simple function to Pretty Print a table in tabular format.</p> <p>Table maybe a list of lists or list of tuples.</p> <p>Justify and column width are optional parameters.</p> Object wrapper class (Python) 2011-01-28T12:40:58-08:00Nichita Utiuhttp://code.activestate.com/recipes/users/4176755/http://code.activestate.com/recipes/577555-object-wrapper-class/ <p style="color: grey"> Python recipe 577555 by <a href="/recipes/users/4176755/">Nichita Utiu</a> (<a href="/recipes/tags/wrapper/">wrapper</a>). </p> <p>This is a wrapper class which, it wraps an object which it then proxies unnhandled getattr calls to.</p> Using dtuple for Flexible Query Result Access (Python) 2001-11-19T20:48:09-08:00Steve Holdenhttp://code.activestate.com/recipes/users/112415/http://code.activestate.com/recipes/81252-using-dtuple-for-flexible-query-result-access/ <p style="color: grey"> Python recipe 81252 by <a href="/recipes/users/112415/">Steve Holden</a> (<a href="/recipes/tags/database/">database</a>). Revision 3. </p> <p>Greg Stein's dtuple module deserves wider recognition. This recipe shows you how to use it with a simple list of database field names.</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> PostgreSQL database backup script (Python) 2011-07-15T08:27:05-07:00Evgeniy.Khttp://code.activestate.com/recipes/users/4178635/http://code.activestate.com/recipes/577793-postgresql-database-backup-script/ <p style="color: grey"> Python recipe 577793 by <a href="/recipes/users/4178635/">Evgeniy.K</a> (<a href="/recipes/tags/backup/">backup</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/postgresql/">postgresql</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/unix/">unix</a>). </p> <p>Simple script to backup some databases from PostgreSQL on unix\linux.</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> 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>