Popular recipes tagged "meta:loc=33"http://code.activestate.com/recipes/tags/meta:loc=33/2017-04-19T18:03:11-07:00ActiveState Code RecipesImplementing function-based callbacks in Python (Python) 2017-04-19T18:03:11-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580787-implementing-function-based-callbacks-in-python/ <p style="color: grey"> Python recipe 580787 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/callback/">callback</a>, <a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/functions/">functions</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/techniques/">techniques</a>). </p> <p>This recipe shows a simple way of implementing callbacks in Python. There are a few ways this can be done. The way shown here uses a simple function-based approach.</p> <p>In a nutshell, a callback can be informally described like this: function <strong>a</strong> calls function <strong>b</strong>, and wants to make <strong>b</strong> run a specific independent chunk of code at some point during <strong>b</strong>'s execution. We want to be able to vary which chunk of code gets called in different calls to <strong>b</strong>, so it cannot be hard-coded inside <strong>b</strong>. So function <strong>a</strong> passes another function, <strong>c</strong>, to <strong>b</strong>, as one argument, and <strong>b</strong> uses that parameter <strong>c</strong> to call the functionality that <strong>a</strong> wants <strong>b</strong> to call. (Function <strong>b</strong> may pass some parameters to the function represented by <strong>c</strong>, when it calls it. These could be either internally generated, passed from <strong>a</strong>, or a combination of both). So, by changing the value of the function <strong>c</strong> that gets passed to <strong>b</strong> (on different calls to <strong>b</strong>), <strong>a</strong> can change what chunk of code <strong>b</strong> calls.</p> <p>More details and full code, description and output here:</p> <p><a href="https://jugad2.blogspot.in/2017/04/implementing-and-using-callbacks-in.html" rel="nofollow">https://jugad2.blogspot.in/2017/04/implementing-and-using-callbacks-in.html</a></p> PyOOCalc - Python Libre/Open Office Calc Interface API (UNO) (Python) 2016-01-10T13:12:53-08:00Yuriihttp://code.activestate.com/recipes/users/4193384/http://code.activestate.com/recipes/579147-pyoocalc-python-libreopen-office-calc-interface-ap/ <p style="color: grey"> Python recipe 579147 by <a href="/recipes/users/4193384/">Yurii</a> (<a href="/recipes/tags/openoffice/">openoffice</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python3/">python3</a>). </p> <p>Create Libre/Open Office Calc documents, reports on Python</p> How to use Python to convert a web page to PDF with a POST request to SelectPdf Online API and save it on the disk (Python) 2015-11-16T14:52:17-08:00SelectPdfhttp://code.activestate.com/recipes/users/4193129/http://code.activestate.com/recipes/579126-how-to-use-python-to-convert-a-web-page-to-pdf-wit/ <p style="color: grey"> Python recipe 579126 by <a href="/recipes/users/4193129/">SelectPdf</a> (<a href="/recipes/tags/api/">api</a>, <a href="/recipes/tags/converter/">converter</a>, <a href="/recipes/tags/htmltopdf/">htmltopdf</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/selectpdf/">selectpdf</a>). </p> <p>This code converts an url to pdf in Python using SelectPdf HTML To PDF REST API through a POST request. The parameters are JSON encoded. The content is saved into a file on the disk.</p> Use PrettyTable and xtopdf to create PDF tables with borders, alignment and padding (Python) 2015-01-24T21:21:27-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579012-use-prettytable-and-xtopdf-to-create-pdf-tables-wi/ <p style="color: grey"> Python recipe 579012 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/data/">data</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/prettytable/">prettytable</a>, <a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/tabular/">tabular</a>, <a href="/recipes/tags/xtopdf/">xtopdf</a>). </p> <p>This recipe shows how to create tabular data in PDF format, supporting neat borders, and alignment and padding of columns, using the Python libraries called PrettyTable and xtopdf.</p> Rock,Paper,Scissors (Python) 2014-12-19T05:39:38-08:00Burak Tandoganhttp://code.activestate.com/recipes/users/4191373/http://code.activestate.com/recipes/578983-rockpaperscissors/ <p style="color: grey"> Python recipe 578983 by <a href="/recipes/users/4191373/">Burak Tandogan</a> (<a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/paper/">paper</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/rock/">rock</a>, <a href="/recipes/tags/scissors/">scissors</a>). Revision 2. </p> <p>A simple game :)</p> Boost::Python callback triggered from Non-Python created threads (C++) 2014-11-20T20:00:27-08:00Tomáš Rampashttp://code.activestate.com/recipes/users/4179816/http://code.activestate.com/recipes/578967-boostpython-callback-triggered-from-non-python-cre/ <p style="color: grey"> C++ recipe 578967 by <a href="/recipes/users/4179816/">Tomáš Rampas</a> (<a href="/recipes/tags/boost/">boost</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>Consider we have virtual/abstract C++ class that's fully implemented in Python. And for some sake of necessity we have a callback method (e.g. as some sort of event) that is being triggered from different thread on C++ side. In such case corresponding callback methods have to manage GIL state via PyGILState_STATE. So the resulting C++ callback class definition will look like follows (notice that Python method calls are wrapped up with GIL state handling code).</p> Continous Fractions (Python) 2014-09-23T14:30:00-07:00juanhttp://code.activestate.com/recipes/users/4190606/http://code.activestate.com/recipes/578936-continous-fractions/ <p style="color: grey"> Python recipe 578936 by <a href="/recipes/users/4190606/">juan</a> . Revision 2. </p> <p>Calculate the continous fraction from a fraction or decimal number</p> Circle Inversion Fractals (Python) 2013-10-30T03:53:31-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/578765-circle-inversion-fractals/ <p style="color: grey"> Python recipe 578765 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>Circle Inversion Fractals (Apollonian Gasket)</p> Archimedes Method for PI (arbitrary precision) (Python) 2013-03-02T21:56:42-08:00Bjorn Madsenhttp://code.activestate.com/recipes/users/4176352/http://code.activestate.com/recipes/578478-archimedes-method-for-pi-arbitrary-precision/ <p style="color: grey"> Python recipe 578478 by <a href="/recipes/users/4176352/">Bjorn Madsen</a> (<a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/pi/">pi</a>, <a href="/recipes/tags/precision/">precision</a>, <a href="/recipes/tags/python3/">python3</a>). </p> <p>The code below incorporates the newly revised python 3.3.0 decimal module to permit arbitrary precision (<a href="http://docs.python.org/3/library/decimal.html%29" rel="nofollow">http://docs.python.org/3/library/decimal.html)</a></p> <p>The recipe was originally by FB35 from <a href="http://code.activestate.com/recipes/576981-archimedes-method-for-calculating-pi/" rel="nofollow">http://code.activestate.com/recipes/576981-archimedes-method-for-calculating-pi/</a> </p> Get full caller name (package.module.function) (Python) 2012-11-30T21:54:46-08:00anatoly techtonikhttp://code.activestate.com/recipes/users/4168147/http://code.activestate.com/recipes/578352-get-full-caller-name-packagemodulefunction/ <p style="color: grey"> Python recipe 578352 by <a href="/recipes/users/4168147/">anatoly techtonik</a> (<a href="/recipes/tags/caller/">caller</a>, <a href="/recipes/tags/debug/">debug</a>, <a href="/recipes/tags/logging/">logging</a>). </p> <p>This function allows to get fully qualified name of the calling function. I expected this field to be available from logging module, but it is not here <a href="http://docs.python.org/2/library/logging.html#logrecord-attributes" rel="nofollow">http://docs.python.org/2/library/logging.html#logrecord-attributes</a> It might be that it is too expensive for performance or just doesn't play well in some situations. I don't know. I use it solely when debugging and it is convenient.</p> <p>Also here: <a href="https://gist.github.com/2151727" rel="nofollow">https://gist.github.com/2151727</a></p> Loaded profiles (Batch) 2013-08-27T16:27:02-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578347-loaded-profiles/ <p style="color: grey"> Batch recipe 578347 by <a href="/recipes/users/4184115/">greg zakharov</a> (<a href="/recipes/tags/profiles/">profiles</a>). Revision 2. </p> <p>This script checks what profiles are loaded with start of system. Launch it without parameters to get help.</p> network (Python) 2012-10-19T11:54:47-07:00el riougahttp://code.activestate.com/recipes/users/4183987/http://code.activestate.com/recipes/578295-network/ <p style="color: grey"> Python recipe 578295 by <a href="/recipes/users/4183987/">el riouga</a> . </p> <p>un petit programme ecrit en mode sur l'utilisation de claase qui convertit une adresse ip en binaire, donne la classe de lip etc. je suis un debutant en python. je pense que vous aller ameliorer mon ptit.presque unitule</p> handling embedded email images in python (Python) 2012-03-02T12:43:51-08:00Gregory Nicholashttp://code.activestate.com/recipes/users/4180332/http://code.activestate.com/recipes/578058-handling-embedded-email-images-in-python/ <p style="color: grey"> Python recipe 578058 by <a href="/recipes/users/4180332/">Gregory Nicholas</a> (<a href="/recipes/tags/appengine/">appengine</a>, <a href="/recipes/tags/email/">email</a>, <a href="/recipes/tags/embedded_image/">embedded_image</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>this code finds all embedded images in the email and replaces them with an appropriate img tag.</p> Evolutionary Algorithm (Generation of Prime Numbers) (Python) 2011-11-27T06:45:00-08:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/577964-evolutionary-algorithm-generation-of-prime-numbers/ <p style="color: grey"> Python recipe 577964 by <a href="/recipes/users/4179768/">Alexander James Wallar</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/example/">example</a>, <a href="/recipes/tags/genetic/">genetic</a>, <a href="/recipes/tags/genetic_algorithm/">genetic_algorithm</a>, <a href="/recipes/tags/genetic_algorithms/">genetic_algorithms</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/number/">number</a>, <a href="/recipes/tags/of/">of</a>, <a href="/recipes/tags/prime/">prime</a>, <a href="/recipes/tags/primelist/">primelist</a>, <a href="/recipes/tags/primes/">primes</a>, <a href="/recipes/tags/theory/">theory</a>). </p> <p>This is an evolutionary algorithm that returns a random list of prime numbers. This code is highly inefficient for a reason. This algorithm is more of a proof of concept that if a prime was a heritable trait, it would not be a desired one. </p> <p>Parameters:</p> <p>isPrime --> n: number to check if it is prime allPrimes --> n: size of list of random primes, m: the primes in the list will be between 0 and m</p> ODE Solver using Euler Method (Python) 2011-04-10T00:30:58-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577647-ode-solver-using-euler-method/ <p style="color: grey"> Python recipe 577647 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>). </p> <p>First and Second Order Ordinary Differential Equation (ODE) Solver using Euler Method.</p> handling embedded email images in python (Python) 2011-10-26T15:31:45-07:00Simeon Shpizhttp://code.activestate.com/recipes/users/4178609/http://code.activestate.com/recipes/577923-handling-embedded-email-images-in-python/ <p style="color: grey"> Python recipe 577923 by <a href="/recipes/users/4178609/">Simeon Shpiz</a> (<a href="/recipes/tags/appengine/">appengine</a>, <a href="/recipes/tags/email/">email</a>, <a href="/recipes/tags/embedded_image/">embedded_image</a>, <a href="/recipes/tags/python/">python</a>). Revision 2. </p> <p>this code finds all embedded images in the email and replaces them with an appropriate img tag.</p> Use Simple Plugins to Seemlessly Modify Modules at Import Time (Python) 2011-07-12T18:55:18-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577740-use-simple-plugins-to-seemlessly-modify-modules-at/ <p style="color: grey"> Python recipe 577740 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/import/">import</a>, <a href="/recipes/tags/modules/">modules</a>). Revision 4. </p> <p>This recipe uses the PEP 302 import hooks to expose all imported modules to devious behavior.</p> <p>Simply put, the module is imported like normal and then passed to a hacker object that gets to do whatever it wants to the module. Then the return value from the hack call is put into sys.modules.</p> <p><a href="http://code.activestate.com/recipes/577741/">Recipe 577741</a> and <a href="http://code.activestate.com/recipes/577742/">recipe 577742</a> are more concrete examples of using this recipe.</p> Arduino Diecimila Board Access Inside A Linux Bash Shell. (Text) 2011-03-30T18:08:11-07:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/577627-arduino-diecimila-board-access-inside-a-linux-bash/ <p style="color: grey"> Text recipe 577627 by <a href="/recipes/users/4177147/">Barry Walker</a> (<a href="/recipes/tags/access/">access</a>, <a href="/recipes/tags/arduino/">arduino</a>, <a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/shell/">shell</a>, <a href="/recipes/tags/text/">text</a>). </p> <p>The "code" in this recipe is a step by step "root" shell command line procedure for testing whether a(n) USB Arduino Dev Board is working or not. To get more recognisable characters displayed it is best to use a potentiometer wired as one end to +5V, the other end to Gnd and thw wiper to ANALOG IN 0. This has been tested on various Linux Distros and kept as simple as possible so that anyone can understand it.</p> <p>The required ?.pde file for the Arduino Board can be found here:-</p> <p><a href="http://code.activestate.com/recipes/577625-arduino-diecimila-board-access-inside-winuae-demo/?in=lang-python" rel="nofollow">http://code.activestate.com/recipes/577625-arduino-diecimila-board-access-inside-winuae-demo/?in=lang-python</a></p> <p>It is issued entirely as Public Domain by B.Walker, G0LCU, 30-03-2011, and you may do with it as you please.</p> <p>Similar assumptions are made as in the URL above.</p> <p>Enjoy finding simple solutions to often very difficult problems... ;o)</p> Fastest way to list all primes below N in python (Python) 2010-07-27T01:57:20-07:00robert.william.hankshttp://code.activestate.com/recipes/users/4174481/http://code.activestate.com/recipes/577331-fastest-way-to-list-all-primes-below-n-in-python/ <p style="color: grey"> Python recipe 577331 by <a href="/recipes/users/4174481/">robert.william.hanks</a> (<a href="/recipes/tags/fast/">fast</a>, <a href="/recipes/tags/fastest/">fastest</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/numbers/">numbers</a>, <a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/primes/">primes</a>, <a href="/recipes/tags/python/">python</a>). Revision 6. </p> <p>I was looking for a fast way to list all primes below n, so far i came up to this with the numpy solution the fastest. It does primes up to 10e6 in 15ms in my old machine, and it is capable of reaching 10e9. </p> File encryption using stream cipher (Python) 2010-03-20T14:53:48-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577090-file-encryption-using-stream-cipher/ <p style="color: grey"> Python recipe 577090 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/encryption/">encryption</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/math/">math</a>). Revision 2. </p> <p>File encryption/decryption using stream cipher. It can encrypt/decrypt any type of file.</p>