Popular recipes tagged "meta:loc=10"http://code.activestate.com/recipes/tags/meta:loc=10/2017-04-28T08:22:08-07:00ActiveState Code RecipesGet disk partition information with psutil (cross-platform) (Python)
2016-12-23T18:05:41-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580737-get-disk-partition-information-with-psutil-cross-p/
<p style="color: grey">
Python
recipe 580737
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/device/">device</a>, <a href="/recipes/tags/disk/">disk</a>, <a href="/recipes/tags/file_system/">file_system</a>, <a href="/recipes/tags/psutil/">psutil</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/sysadmin/">sysadmin</a>, <a href="/recipes/tags/system/">system</a>).
</p>
<p>This is a recipe that shows how to easily get disk partition information, in a cross-platform manner (for the supported OSes), from your computer's operating system, using the psutil library for Python.</p>
Count lines and words in a text file (Batch)
2017-04-28T08:22:08-07:00Antoni Gualhttp://code.activestate.com/recipes/users/4182514/http://code.activestate.com/recipes/580791-count-lines-and-words-in-a-text-file/
<p style="color: grey">
Batch
recipe 580791
by <a href="/recipes/users/4182514/">Antoni Gual</a>
(<a href="/recipes/tags/word_count/">word_count</a>).
Revision 2.
</p>
<p>Returns number of lines and words (separed by spaces) of a text file dragged to the script. Does'nt count blank lines. </p>
Validate product (Python)
2014-10-05T19:17:56-07:00Savu Andreeahttp://code.activestate.com/recipes/users/4190914/http://code.activestate.com/recipes/578947-validate-product/
<p style="color: grey">
Python
recipe 578947
by <a href="/recipes/users/4190914/">Savu Andreea</a>
.
</p>
<p>Necessary in commerce</p>
Python DBAPI 2 "qmark" to "named" paramstyle (Python)
2012-12-14T19:39:04-08:00Steve Howehttp://code.activestate.com/recipes/users/4184602/http://code.activestate.com/recipes/578380-python-dbapi-2-qmark-to-named-paramstyle/
<p style="color: grey">
Python
recipe 578380
by <a href="/recipes/users/4184602/">Steve Howe</a>
(<a href="/recipes/tags/dbapi/">dbapi</a>, <a href="/recipes/tags/named/">named</a>, <a href="/recipes/tags/paramstyle/">paramstyle</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/qmark/">qmark</a>, <a href="/recipes/tags/sqlite/">sqlite</a>, <a href="/recipes/tags/sqlite3/">sqlite3</a>).
</p>
<p>Allows to use "named"-style params with drivers that support only "qmark"-style, as sqlite3.</p>
Loaded assemblies (Ruby)
2012-11-19T08:53:23-08:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578341-loaded-assemblies/
<p style="color: grey">
Ruby
recipe 578341
by <a href="/recipes/users/4184115/">greg zakharov</a>
(<a href="/recipes/tags/assemblies/">assemblies</a>, <a href="/recipes/tags/ironruby/">ironruby</a>).
</p>
<p>Sometimes (working with IronRuby with interactive mode) it's very useful to know what assemblies was loaded. This sample shows how to do it.</p>
How to Mutate a Float (Python)
2012-05-14T16:49:13-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578132-how-to-mutate-a-float/
<p style="color: grey">
Python
recipe 578132
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/ctypes/">ctypes</a>, <a href="/recipes/tags/experiment/">experiment</a>, <a href="/recipes/tags/float/">float</a>, <a href="/recipes/tags/structure/">structure</a>).
</p>
<p>This is just an experiment to test my mental understanding of Python. It should not be used in any code as is violates the design principle that floats are to be immutable. The code also abuses <code>ctypes</code> and an understanding of how <code>floats</code> are currently arranged in memory. <code>set_float</code> is not guaranteed to work properly on any system, and may fail to work in the future if the data's arrangement changes.</p>
Context manager to prevent calling code from catching exceptions (Python)
2012-06-17T09:20:56-07:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/577863-context-manager-to-prevent-calling-code-from-catch/
<p style="color: grey">
Python
recipe 577863
by <a href="/recipes/users/2033964/">Oren Tirosh</a>
(<a href="/recipes/tags/context_manager/">context_manager</a>, <a href="/recipes/tags/exception/">exception</a>).
</p>
<p>The following context manager causes any exceptions raised inside it to print a stack trace and exit immediately. The calling scope is not given a chance to catch the exception.</p>
Teach the hashbang header new tricks using a dual mode shell/python script (Python)
2011-08-21T07:27:05-07:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/577851-teach-the-hashbang-header-new-tricks-using-a-dual-/
<p style="color: grey">
Python
recipe 577851
by <a href="/recipes/users/2033964/">Oren Tirosh</a>
(<a href="/recipes/tags/hashbang/">hashbang</a>, <a href="/recipes/tags/interpreter/">interpreter</a>, <a href="/recipes/tags/options/">options</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/sh/">sh</a>, <a href="/recipes/tags/shell/">shell</a>).
</p>
<p>This dual-mode script is both a Posix shell script and a python script. The shell part looks like a triple-quoted string to the Python interpreter. The shell does not reach anything after the exec statement.</p>
Calculate Trailing Zeroes in a Factorial (Python)
2011-08-15T17:23:03-07:00Stijn de Graafhttp://code.activestate.com/recipes/users/4178055/http://code.activestate.com/recipes/577844-calculate-trailing-zeroes-in-a-factorial/
<p style="color: grey">
Python
recipe 577844
by <a href="/recipes/users/4178055/">Stijn de Graaf</a>
(<a href="/recipes/tags/calculate/">calculate</a>, <a href="/recipes/tags/calculation/">calculation</a>, <a href="/recipes/tags/factorial/">factorial</a>, <a href="/recipes/tags/trailing/">trailing</a>, <a href="/recipes/tags/zero/">zero</a>, <a href="/recipes/tags/zeroes/">zeroes</a>).
</p>
<p>Calculates the number of trailing zeroes on the end of x! when the user inputs x.</p>
Pickle to/from socket (Python)
2011-04-19T22:22:53-07:00pavelhttp://code.activestate.com/recipes/users/4171837/http://code.activestate.com/recipes/577667-pickle-tofrom-socket/
<p style="color: grey">
Python
recipe 577667
by <a href="/recipes/users/4171837/">pavel</a>
(<a href="/recipes/tags/pickle/">pickle</a>, <a href="/recipes/tags/socket/">socket</a>).
Revision 2.
</p>
<p>It's useful for transfering objects through socket, when doing communication between processes or networking.</p>
C# Surface Normal (Java)
2010-08-06T21:00:07-07:00John Hurlimanhttp://code.activestate.com/recipes/users/4174599/http://code.activestate.com/recipes/577348-c-surface-normal/
<p style="color: grey">
Java
recipe 577348
by <a href="/recipes/users/4174599/">John Hurliman</a>
(<a href="/recipes/tags/csharp/">csharp</a>, <a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/normal/">normal</a>).
</p>
<p>Calculates a surface normal given three position vectors in clockwise order. Written in C#</p>
Improved range function (Python)
2010-06-07T01:50:20-07:00Sunjay Varmahttp://code.activestate.com/recipes/users/4174115/http://code.activestate.com/recipes/577256-improved-range-function/
<p style="color: grey">
Python
recipe 577256
by <a href="/recipes/users/4174115/">Sunjay Varma</a>
(<a href="/recipes/tags/float/">float</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/range/">range</a>, <a href="/recipes/tags/replace/">replace</a>).
Revision 2.
</p>
<p>While using the built-in range function a while ago. I found an odd (perhaps bug) where the range function couldn't use float steps. I am not sure if that was intended for simplicity or not, but I wrote my own range function that goes beyond that anyway.</p>
query (Python)
2010-03-09T18:34:05-08:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577099-query/
<p style="color: grey">
Python
recipe 577099
by <a href="/recipes/users/4173505/">Trent Mick</a>
(<a href="/recipes/tags/ask/">ask</a>, <a href="/recipes/tags/cli/">cli</a>, <a href="/recipes/tags/query/">query</a>, <a href="/recipes/tags/raw_input/">raw_input</a>).
</p>
<p>Ask/prompt the user for a short piece of data. <a href="http://code.activestate.com/recipes/577098/">Recipe 577098</a> is a much more generic/functional (but longer) take on this.</p>
Depth first search generator (Python)
2009-11-09T04:46:21-08:00Paul W. Millerhttp://code.activestate.com/recipes/users/4172186/http://code.activestate.com/recipes/576946-depth-first-search-generator/
<p style="color: grey">
Python
recipe 576946
by <a href="/recipes/users/4172186/">Paul W. Miller</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/graphs/">graphs</a>).
Revision 7.
</p>
<p>This is the standard iterative DFS code modified to yield the vertices visited, so you don't have to pass a function into the DFS routine to process them. Note that this code is not quite complete... you'll need to define the function neighbors (v) based on your graph representation.</p>
Flattened List (Python)
2009-04-27T18:41:21-07:00marlonamorhttp://code.activestate.com/recipes/users/4169863/http://code.activestate.com/recipes/576719-flattened-list/
<p style="color: grey">
Python
recipe 576719
by <a href="/recipes/users/4169863/">marlonamor</a>
(<a href="/recipes/tags/flat/">flat</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/itertools/">itertools</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/nested/">nested</a>).
Revision 2.
</p>
<p>This flattenizes any nested level list. I couln't find this in itertools module so I wrote it. Python 3 may be required.</p>
Unique lines from a text file, case sensitive or insensitive (Python)
2009-04-05T19:58:50-07:00nickhttp://code.activestate.com/recipes/users/4169647/http://code.activestate.com/recipes/576713-unique-lines-from-a-text-file-case-sensitive-or-in/
<p style="color: grey">
Python
recipe 576713
by <a href="/recipes/users/4169647/">nick</a>
(<a href="/recipes/tags/uniq/">uniq</a>, <a href="/recipes/tags/unique_lines/">unique_lines</a>).
</p>
<p>Spits out sorted, deduplicated lines.</p>
Generating correlated random numbers (Python)
2008-09-21T21:21:52-07:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/576512-generating-correlated-random-numbers/
<p style="color: grey">
Python
recipe 576512
by <a href="/recipes/users/4166965/">Kaushik Ghose</a>
(<a href="/recipes/tags/matplotlib/">matplotlib</a>, <a href="/recipes/tags/random_number/">random_number</a>).
</p>
<p>From this great <a href="http://www.sitmo.com/doc/Generating_Correlated_Random_Numbers">tutorial</a></p>
<p>For two corelated variables, the formula is much as one would get from intuition about the meaning of correlation with some twist due to normalizing the standard deviation:
$X_3 = \alpha X_1 + \sqrt{1-\alpha^2} X_2$
Where $X_1$ and $X_2$ are two independent random variables, and $\alpha$ is the coefficient of correlation between $X_1$ and $X_3$.</p>
<p>In a more general sense: <br />
Let $C$ be the correlation matrix desired. Let $X_1, X_2..., X_N$ be $N$ independent random variables arranged in a row matrix $R = [X_1, X_2,....,X_N]$. Then
$Q = RU$
where
$U^TU = C$
gives us $N$ random variables $Q = [Y_1, Y_2, ..., Y_N]$ with the required property.</p>
Case-insensitive string replacement (Python)
2008-03-26T22:07:55-07:00Christopher Neugebauerhttp://code.activestate.com/recipes/users/4136800/http://code.activestate.com/recipes/552726-case-insensitive-string-replacement/
<p style="color: grey">
Python
recipe 552726
by <a href="/recipes/users/4136800/">Christopher Neugebauer</a>
(<a href="/recipes/tags/text/">text</a>).
Revision 2.
</p>
<p>Python does not have case-insensitive string replacement built into the default string class. This class provides a method that allows you to do this.</p>
Using PyXPCOM to handle IDN (Python)
2006-12-22T20:49:12-08:00Sanghyeon Seohttp://code.activestate.com/recipes/users/990805/http://code.activestate.com/recipes/499340-using-pyxpcom-to-handle-idn/
<p style="color: grey">
Python
recipe 499340
by <a href="/recipes/users/990805/">Sanghyeon Seo</a>
.
</p>
<p>PyXPCOM gives Python access to Mozilla's XPCOM, cross-platform component object model.</p>
<p>This recipe demonstrates using PyXPCOM to access Mozilla's IDN service.</p>
Count PDF pages (Python)
2013-07-11T06:15:27-07:00Dirk Holtwickhttp://code.activestate.com/recipes/users/636691/http://code.activestate.com/recipes/496837-count-pdf-pages/
<p style="color: grey">
Python
recipe 496837
by <a href="/recipes/users/636691/">Dirk Holtwick</a>
(<a href="/recipes/tags/files/">files</a>).
</p>
<p>A simple way to count the pages of a PDF the pure Python way.</p>