Popular recipes tagged "meta:loc=52"http://code.activestate.com/recipes/tags/meta:loc=52/2017-06-18T17:43:47-07:00ActiveState Code RecipesHow to Create a PDF with a Caustic Drawing (Python)
2017-06-18T17:43:47-07:00Jorj X. McKiehttp://code.activestate.com/recipes/users/4193772/http://code.activestate.com/recipes/580806-how-to-create-a-pdf-with-a-caustic-drawing/
<p style="color: grey">
Python
recipe 580806
by <a href="/recipes/users/4193772/">Jorj X. McKie</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>).
</p>
<p>Just a little demo on how to create simple drawings with PyMuPDF.</p>
<p>This script simulates what you see looking into your coffee mug, early in the morning after a long night of programming ...</p>
Convert JSON to PDF with Python and xtopdf (Python)
2014-12-10T18:02:14-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/578979-convert-json-to-pdf-with-python-and-xtopdf/
<p style="color: grey">
Python
recipe 578979
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/json/">json</a>, <a href="/recipes/tags/pdfwriter/">pdfwriter</a>, <a href="/recipes/tags/sys/">sys</a>).
</p>
<p>This recipe show the basic steps needed to convert JSON input to PDF output, using Python and xtopdf, a PDF creation toolkit. xtopdf is itself written in Pytho, and uses the ReportLab toolkit internally.</p>
<p>We set up some needed values, such as the output PDF file name, the font name and size, the header and footer, and the input lines for the body of the PDF output; all these values are passed in JSON format (in a single dictionary) to a function that uses those values to generate a PDF file with the desired content.</p>
<p>The code is intentionally kept simple so as to require the least amount of code needed to demonstrate the techniques involved. But it can be generalized or extended to more complex situations.</p>
Urllib handler for Amazon S3 buckets (Python)
2014-11-06T18:31:44-08:00Andrea Corbellinihttp://code.activestate.com/recipes/users/4186880/http://code.activestate.com/recipes/578957-urllib-handler-for-amazon-s3-buckets/
<p style="color: grey">
Python
recipe 578957
by <a href="/recipes/users/4186880/">Andrea Corbellini</a>
(<a href="/recipes/tags/aws/">aws</a>, <a href="/recipes/tags/s3/">s3</a>, <a href="/recipes/tags/urllib/">urllib</a>).
</p>
<p>This is a handler for the standard <a href="https://docs.python.org/dev/library/urllib.request.html">urllib.request</a> module capable of opening buckets stored on <a href="http://aws.amazon.com/s3/">Amazon S3</a>.</p>
<p>Here is an usage example:</p>
<pre class="prettyprint"><code>>>> from urllib.request import build_opener
>>> opener = build_opener(S3Handler)
>>> response = opener.open('s3://bucket-name/key-name')
>>> response.read()
b'contents'
</code></pre>
Python list of files and their path from folders and subfolders (Python)
2013-06-10T19:07:46-07:00Sam Khanhttp://code.activestate.com/recipes/users/4185025/http://code.activestate.com/recipes/578557-python-list-of-files-and-their-path-from-folders-a/
<p style="color: grey">
Python
recipe 578557
by <a href="/recipes/users/4185025/">Sam Khan</a>
(<a href="/recipes/tags/file_io/">file_io</a>).
</p>
<p>Look into a folder along with subfolders for a file starting with or ending with certain characters. </p>
Password Generator (mkpasswd) (Python)
2013-07-31T23:23:02-07:00James Millshttp://code.activestate.com/recipes/users/4167757/http://code.activestate.com/recipes/578468-password-generator-mkpasswd/
<p style="color: grey">
Python
recipe 578468
by <a href="/recipes/users/4167757/">James Mills</a>
(<a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/mkpasswd/">mkpasswd</a>, <a href="/recipes/tags/password/">password</a>, <a href="/recipes/tags/secure/">secure</a>).
Revision 3.
</p>
<p>Since everyone is posting one of these, I thought I'd post mine. I wrote this many years ago and use it everywhere.</p>
A class decorator for creating named tuples (Python)
2013-02-14T19:58:13-08:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/578456-a-class-decorator-for-creating-named-tuples/
<p style="color: grey">
Python
recipe 578456
by <a href="/recipes/users/4177816/">Eric Snow</a>
(<a href="/recipes/tags/metaprogramming/">metaprogramming</a>, <a href="/recipes/tags/namedtuple/">namedtuple</a>).
Revision 3.
</p>
<p>This class decorator factory is useful for replacing the following:</p>
<pre class="prettyprint"><code>class MyTuple(namedtuple('MyTuple', "a b c")):
"""Something special."""
@classmethod
def from_defaults(cls, a, b=None, c=5):
return cls(a, b, c)
</code></pre>
<p>or even:</p>
<pre class="prettyprint"><code>class MyTuple(namedtuple('MyTuple', "a b c")):
"""Something special."""
def __new__(cls, a, b=None, c=5):
return super().__new__(cls, a, b, c)
</code></pre>
<p>with this:</p>
<pre class="prettyprint"><code>@as_namedtuple("a b c", None, c=5)
class MyTuple:
"""Something special."""
</code></pre>
<p>I found that I often subclass named tuples to add on some functionality or even just a nice docstring. Plus with the class syntax there's no missing that a class is bound to the name (and it's a little easier to search for the definition). When you subclass a named tuple the boilerplate involved really jumps out.</p>
<p>One of the main reasons Adding support for defaults to namedtuple would mitigate the need for that functionality here, but I'm not going to hold my breath on that.</p>
<p>One nice (though minor) thing is that you don't have to repeat the name when defining the namedtuple.</p>
First Class Enums in Python (Python)
2013-03-07T08:23:46-08:00Deepakhttp://code.activestate.com/recipes/users/4183429/http://code.activestate.com/recipes/578482-first-class-enums-in-python/
<p style="color: grey">
Python
recipe 578482
by <a href="/recipes/users/4183429/">Deepak</a>
(<a href="/recipes/tags/programs/">programs</a>).
</p>
<p>True immutable symbolic enumeration with qualified value access.</p>
Create replica of the existing directory structure with only specified extension files. (Python)
2012-11-16T08:30:31-08:00Achal Rastogihttp://code.activestate.com/recipes/users/4183889/http://code.activestate.com/recipes/578303-create-replica-of-the-existing-directory-structure/
<p style="color: grey">
Python
recipe 578303
by <a href="/recipes/users/4183889/">Achal Rastogi</a>
(<a href="/recipes/tags/bioinformatics/">bioinformatics</a>, <a href="/recipes/tags/biology/">biology</a>, <a href="/recipes/tags/directory/">directory</a>, <a href="/recipes/tags/fileextensions/">fileextensions</a>, <a href="/recipes/tags/replica/">replica</a>).
Revision 2.
</p>
<p>The program creates a replica of existing directory structure with specified extension files only. The program maintains the directory-sub-directory architecture while executing. User has to provide two arguments, First the path of existing directory,whose replica has to be created and Second, the path of directory, where the replica has to be created.
This program is developed to fetch files with ".pl" (perl) extension, while maintaining the directory-sub-directory architecture. User can modify it, with his/her interested file extension, like ".txt", ".doc", etc.</p>
Generator of combinations without replacement for a sequence using dynamic programming (Python)
2011-06-21T09:11:19-07:00Filippo Squillacehttp://code.activestate.com/recipes/users/4174931/http://code.activestate.com/recipes/577766-generator-of-combinations-without-replacement-for-/
<p style="color: grey">
Python
recipe 577766
by <a href="/recipes/users/4174931/">Filippo Squillace</a>
.
</p>
<p>From the previous recipe <a href="http://code.activestate.com/recipes/577764-combinations-of-a-sequence-without-replacement-usi/" rel="nofollow">http://code.activestate.com/recipes/577764-combinations-of-a-sequence-without-replacement-usi/</a> we can easily build a generator that return all the K-combinations without replacement.</p>
Function Pipelines (Python)
2011-05-15T19:38:13-07:00Alexandre Zanihttp://code.activestate.com/recipes/users/4177180/http://code.activestate.com/recipes/577696-function-pipelines/
<p style="color: grey">
Python
recipe 577696
by <a href="/recipes/users/4177180/">Alexandre Zani</a>
(<a href="/recipes/tags/events/">events</a>, <a href="/recipes/tags/pipelining/">pipelining</a>, <a href="/recipes/tags/wsgi/">wsgi</a>).
</p>
<p>Pipelines can be used to chain a series of functions to process a piece of data. For instance you can build a pipeline to process web requests easily including some middleware for instance.</p>
Axis POJO stripper. (Python)
2011-01-04T12:05:12-08:00Chris Wolfhttp://code.activestate.com/recipes/users/4173108/http://code.activestate.com/recipes/577533-axis-pojo-stripper/
<p style="color: grey">
Python
recipe 577533
by <a href="/recipes/users/4173108/">Chris Wolf</a>
(<a href="/recipes/tags/axis/">axis</a>, <a href="/recipes/tags/pojo/">pojo</a>, <a href="/recipes/tags/wsdl2java/">wsdl2java</a>).
</p>
<p>This utility lets us use the 'wsdl2java' utility from Axis-1 to
generate POJOs from the schema embedded in a WSDL. If we just want
POJOs without the Axis marshalling/unmarshalling code, then this
script will strip out the Axis code, leaving just the POJOs.</p>
Simple tabulator (Python)
2010-11-09T12:50:06-08:00Noufal Ibrahimhttp://code.activestate.com/recipes/users/4173873/http://code.activestate.com/recipes/577458-simple-tabulator/
<p style="color: grey">
Python
recipe 577458
by <a href="/recipes/users/4173873/">Noufal Ibrahim</a>
(<a href="/recipes/tags/parsing/">parsing</a>, <a href="/recipes/tags/text_processing/">text_processing</a>).
</p>
<p>This is a simple script to covert a top to bottom list of items into a left to right list.</p>
<pre class="prettyprint"><code> a
b
c
d
e
f
g
h
i
j
k
l
m
</code></pre>
<p>into</p>
<pre class="prettyprint"><code> a b c d e f g
h i j k l m
</code></pre>
<p>A few command line options allow some amount of customisation. </p>
Benford's Law demo (Python)
2010-10-19T10:56:51-07:00Glenn Hutchingshttp://code.activestate.com/recipes/users/4175415/http://code.activestate.com/recipes/577431-benfords-law-demo/
<p style="color: grey">
Python
recipe 577431
by <a href="/recipes/users/4175415/">Glenn Hutchings</a>
(<a href="/recipes/tags/benford/">benford</a>, <a href="/recipes/tags/matplotlib/">matplotlib</a>, <a href="/recipes/tags/plotting/">plotting</a>).
</p>
<p>Here's a simple program to demonstrate <a href="http://en.wikipedia.org/wiki/Benford%27s_law">Benford's Law</a>, which also shows the simple power of <a href="http://matplotlib.sourceforge.net">matplotlib</a>. It reads from a bunch of files (or stdin, if none specified), extracts the leading digits of all number-like strings found, and plots the distribution in a window together with the expected result if Benford's law applies.</p>
Maclaurin's_series_sin (Python)
2010-07-07T12:05:33-07:00Fouad Teniouhttp://code.activestate.com/recipes/users/4155345/http://code.activestate.com/recipes/577287-maclaurins_series_sin/
<p style="color: grey">
Python
recipe 577287
by <a href="/recipes/users/4155345/">Fouad Teniou</a>
(<a href="/recipes/tags/mathematics/">mathematics</a>).
</p>
<p>C. Maclaurin. A Scottish mathematician gained his master degree at age 17, and his major mathematics' work arise from his special knowledge in Newton's ideas and the formulation of Newton's methods.</p>
<p>However, C. Maclaurin also contributed to the astronomy science and helped to improve maps and invented some mechanical devices.</p>
<p>My mathematics python's programs is a set of Maclaurin's series to compute some of the most important functions in calculus.</p>
<p>Though, the computation of an infinite sum which give the value of a function in terms of the derivatives evaluated at a special case where x0 = 0,in contrast with Taylor series. </p>
<p>It is assumed that the angle is expressed in radian while using the Maclaurin's series to approximate the trigonometric functions sine and cosine, thus, converting the angle into degrees in my program could be useful for students who prefer using degrees.</p>
Maclaurin's_series_cosh-1(x) (Python)
2010-09-28T11:53:08-07:00Fouad Teniouhttp://code.activestate.com/recipes/users/4155345/http://code.activestate.com/recipes/577400-maclaurins_series_cosh-1x/
<p style="color: grey">
Python
recipe 577400
by <a href="/recipes/users/4155345/">Fouad Teniou</a>
(<a href="/recipes/tags/mathematics/">mathematics</a>).
</p>
<p>C. Maclaurin. A Scottish mathematician gained his master degree at age 17, and his major mathematics' work arise from his special knowledge in Newton's ideas and the formulation of Newton's methods.</p>
<p>However, C. Maclaurin also contributed to the astronomy science and helped to improve maps and invented some mechanical devices .</p>
<p>My mathematics python's programs is a set of Maclaurin's series to compute some of the most important functions in calculus.</p>
<p>Though, the computation of an infinite sum which give the value of a function in terms of the derivatives evaluated at a special case where x0 = 0,in contrast with Taylor's series. </p>
<p>Natural logarithms are used to compute inverse hyperbolic functions.</p>
Extension to Python 3 Counter class (Python)
2010-08-18T21:02:41-07:00Paddy McCarthyhttp://code.activestate.com/recipes/users/398009/http://code.activestate.com/recipes/577362-extension-to-python-3-counter-class/
<p style="color: grey">
Python
recipe 577362
by <a href="/recipes/users/398009/">Paddy McCarthy</a>
(<a href="/recipes/tags/bag/">bag</a>, <a href="/recipes/tags/counter/">counter</a>, <a href="/recipes/tags/multiset/">multiset</a>, <a href="/recipes/tags/python/">python</a>).
Revision 3.
</p>
<p>Adds symmetric difference and cartesian product to the Counter class. I had a use case for the former and Raymond H. asked about the latter so I coded the latter from its Wikipedia description.</p>
Maclaurin's_series_cos (Python)
2010-07-07T11:56:52-07:00Fouad Teniouhttp://code.activestate.com/recipes/users/4155345/http://code.activestate.com/recipes/577285-maclaurins_series_cos/
<p style="color: grey">
Python
recipe 577285
by <a href="/recipes/users/4155345/">Fouad Teniou</a>
(<a href="/recipes/tags/mathematics/">mathematics</a>).
Revision 2.
</p>
<p>Maclaurin. A Scottish mathematician gained his master degree at age 17, and his major mathematics' work arise from his special knowledge in Newton's ideas and the formulation of Newton's methods.</p>
<p>However, C. Maclaurin also contributed to the astronomy science and helped to improve maps and invented some mechanical devices .</p>
<p>My mathematics python's programs is a set of Maclaurin's series to compute some of the most important functions in calculus.</p>
<p>Though, the computation of an infinite sum which give the value of a function in terms of the derivatives evaluated at a special case where x0 = 0,in contrast with Taylor series. </p>
<p>It is assumed that the angle is expressed in radian while using the Maclaurin's series to approximate the trigonometric functions sine and cosine, thus, converting the angle into degrees in my program could be useful for students who prefer using degrees.</p>
Maclaurin's_series_cos(2x) (Python)
2010-08-03T12:23:33-07:00Fouad Teniouhttp://code.activestate.com/recipes/users/4155345/http://code.activestate.com/recipes/577344-maclaurins_series_cos2x/
<p style="color: grey">
Python
recipe 577344
by <a href="/recipes/users/4155345/">Fouad Teniou</a>
(<a href="/recipes/tags/mathematics/">mathematics</a>).
Revision 2.
</p>
<p>C. Maclaurin. A Scottish mathematician gained his master degree at age 17, and his major mathematics' work arise from his special knowledge in Newton's ideas and the formulation of Newton's methods.</p>
<p>However, C. Maclaurin also contributed to the astronomy science and helped to improve maps and invented some mechanical devices .</p>
<p>My mathematics python's programs is a set of Maclaurin's series to compute some of the most important functions in calculus.</p>
<p>Though, the computation of an infinite sum which give the value of a function in terms of the derivatives evaluated at a special case where x0 = 0,in contrast with Taylor series. </p>
<p>There are several ways of finding Maclaurin’s series, and I used the multiplication and the division to develop my own maclaurin’s series for cos(2x) and cos²(x).</p>
<p>Cos(2x) = 2cos²(x) - 1 and cos²(x) = (cos(2x)+1)/2</p>
<p>However, integration and differentiation could also be used to find Maclaurin’s series. </p>
Komodo Macro - Copy to clipboard code selection in HTML format (JavaScript)
2010-04-14T07:17:59-07:00Davide Ficanohttp://code.activestate.com/recipes/users/4166571/http://code.activestate.com/recipes/577193-komodo-macro-copy-to-clipboard-code-selection-in-h/
<p style="color: grey">
JavaScript
recipe 577193
by <a href="/recipes/users/4166571/">Davide Ficano</a>
(<a href="/recipes/tags/clipboard/">clipboard</a>, <a href="/recipes/tags/html/">html</a>, <a href="/recipes/tags/komodo/">komodo</a>, <a href="/recipes/tags/macro/">macro</a>).
</p>
<p>This macro makes easy to paste on documents (MS Office, Open Office, iWorks Pages) code snippets with syntax highlight and/or line numbers.</p>
<p>This macro copies the selected code text or whole document if no selection is present on clipboard in HTML format.</p>
<p>On OSX you need at least Komodo 6 because previous versions don't support data flavors</p>
Validating Emails (PHP)
2011-06-13T15:10:47-07:00Jonathan Fenechhttp://code.activestate.com/recipes/users/4169413/http://code.activestate.com/recipes/577198-validating-emails/
<p style="color: grey">
PHP
recipe 577198
by <a href="/recipes/users/4169413/">Jonathan Fenech</a>
(<a href="/recipes/tags/emails/">emails</a>, <a href="/recipes/tags/validating/">validating</a>).
Revision 2.
</p>
<p>a small script that can be used for Validating Emails on </p>
<ul>
<li>Login pages</li>
<li>forums </li>
</ul>
<p>changed to preg_match from eregi function. now scripted validates everything typed in input boxes </p>
<p>Old Code = </p>
<pre class="prettyprint"><code> foreach($Email as $Emails)
{ // checks Emails - .net , .com , .au & etc
if(eregi("[a-zA-Z0-9]@+[a-z].{1,}com$",trim($Emails))|| eregi(
"[a-zA-Z0-9]@+[a-z].{1,}net$",trim($Emails)))
</code></pre>
<p>New Code = </p>
<pre class="prettyprint"><code> foreach($Email as $Emails)
{ // checks Emails - .net , .com , .au & etc
if(!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z]
[0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $Emails))
</code></pre>
<p>i changed from eregi function to preg_match function because eregi was taking out of PHP version 5.3.0 and 5.3.5.</p>