Design patterns - Python's use of __new__ and __init__? Obey the Testing Goat! Timing and Profiling in IPython - PyNash. Timing and profiling code is all sorts of useful, and it’s also just good ol’ fashioned fun (and sometimes surprising!).
In this post, I’ll introduce how to do the following through IPython magic functions: %time & %timeit: See how long a script takes to run (one time, or averaged over a bunch of runs). %prun: See how long it took each function in a script to run. %lprun: See how long it took each line in a function to run. %mprun & %memit: See how much memory a script uses (line-by-line, or averaged over a bunch of runs). Please make sure you’re running IPython 0.11 or greater. . $ pip install ipython $ ipython --version 0.13.1 Most of the functionality we’ll work with is included in the standard library, but if you’re interested in line-by-line or memory profiling, go ahead and run through this setup. . $ pip install line-profiler $ pip install psutil $ pip install memory_profiler Create the following IPython extention files with the contents below to define the magic functions: Python gnupg (GPG) example. The Hitchhiker’s Guide to Python!
31.12. dis — Disassembler for Python bytecode. Source code: Lib/dis.py The dis module supports the analysis of CPython bytecode by disassembling it.
The CPython bytecode which this module takes as an input is defined in the file Include/opcode.h and used by the compiler and the interpreter. CPython implementation detail: Bytecode is an implementation detail of the CPython interpreter! No guarantees are made that bytecode will not be added, removed, or changed between versions of Python. Use of this module should not be considered to work across Python VMs or Python releases. Example: Given the function myfunc(): def myfunc(alist): return len(alist) the following command can be used to get the disassembly of myfunc(): 9. Classes — Python 3.4.2 documentation. Compared with other programming languages, Python’s class mechanism adds classes with a minimum of new syntax and semantics.
It is a mixture of the class mechanisms found in C++ and Modula-3. Python classes provide all the standard features of Object Oriented Programming: the class inheritance mechanism allows multiple base classes, a derived class can override any methods of its base class or classes, and a method can call the method of a base class with the same name. Objects can contain arbitrary amounts and kinds of data. As is true for modules, classes partake of the dynamic nature of Python: they are created at runtime, and can be modified further after creation. In C++ terminology, normally class members (including the data members) are public (except see below Private Variables), and all member functions are virtual.
(Lacking universally accepted terminology to talk about classes, I will make occasional use of Smalltalk and C++ terms. Good examples of python-memcache (memcached) being used in Python?
Python - How can I get a SQLAlchemy ORM object's previous state after a db update? Django. Python snppets doc. Flask. Python script to delete merged git branches. One of the great things about git is how fast it is.
You can create a new branch, or switch to another branch, almost as fast as you can type the command. This tends to lower the impedance of branching. As a result, many individuals and teams will naturally converge on a process where they create many, many branches. If you're like me, you may have 30 branches at any given time. This can make viewing all the branches unwieldy. The basic use case is that you want to delete any branches that are already merged into master. From subprocess import check_outputimport sys def get_merged_branches(): ''' a list of merged branches, not couting the current branch or master ''' raw_results = check_output('git branch --merged upstream/master', shell=True) return [b.strip() for b in raw_results.split('\n') if b.strip() and not b.startswith('*') and b.strip() !
Face Recognition with Python, in under 25 lines of code - Real Python. The following is a guest post by Shantnu Tiwari, who has worked in the low level/embedded domain for ten years.
Shantnu suffered at the hands of C/C++ for several years before he discovered Python, and it felt like a breath of fresh air. He is now trying to share his love. In this post we’ll look at a surprisingly simple way to get started with face recognition using Python and the open source library OpenCV. OpenCV OpenCV is the most popular library for computer vision. OpenCV uses machine learning algorithms to search for faces within a picture. For something like a face, you might have 6,000 or more classifiers, all of which must match for a face to be detected (within error limits, of course). To get around this, OpenCV uses cascades. Like a series of waterfalls, the OpenCV cascade breaks the problem of detecting faces into multiple stages. Cascades in practice Though the theory may sound complicated, in practice it is quite easy.
Installing OpenCV Understanding the code.