background preloader

Python

Facebook Twitter

Learn Object Oriented Programming in Python - Composition - Eduonix Blog. Sorting, searching and algorithm analysis — Object-Oriented Programming in Python 1 documentation. Introduction¶ We have learned that in order to write a computer program which performs some task we must construct a suitable algorithm. However, whatever algorithm we construct is unlikely to be unique – there are likely to be many possible algorithms which can perform the same task.

Are some of these algorithms in some sense better than others? Algorithm analysis is the study of this question. In this chapter we will analyse four algorithms; two for each of the following common tasks: sorting: ordering a list of valuessearching: finding the position of a value within a list Algorithm analysis should begin with a clear statement of the task to be performed. Although there are many ways that algorithms can be compared, we will focus on two that are of primary importance to many data processing algorithms: time complexity: how the number of steps required depends on the size of the inputspace complexity: how the amount of extra memory or storage required depends on the size of the input Note. 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. 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 Next, create an IPython profile and extensions directory where we’ll configure a couple of missing magic functions: ~/.ipython/extensions/line_profiler_ext.py ~/.ipython/extensions/memory_profiler_ext.py %time %prun. Python gnupg (GPG) example. Python gnupg (GPG) example python-gnupg is a Python package for encrypting and decrypting strings or files using GNU Privacy Guard (GnuPG or GPG). GPG is an open source alternative to Pretty Good Privacy (PGP). A popular use of GPG and PGP is encrypting email. For more information, see the python-gnupg documentation. Another option for encrypting data from Python is keyczar. Install¶ This installs the Ubuntu GPG package, creates a test user, and installs the Python package, python-gnupg.

. $ sudo apt-get install gnupg $ sudo adduser testgpguser $ sudo su testgpguser $ cd $ virtualenv --no-site-packages venv $ source venv/bin/activate $ pip install python-gnupg Generate a key¶ This creates a GPG key. . $ sudo apt-get install rng-tools import osimport gnupg os.system('rm -rf /home/testgpguser/gpghome')gpg = gnupg.GPG(gnupghome='/home/testgpguser/gpghome')input_data = gpg.gen_key_input( name_email='testgpguser@mydomain.com', passphrase='my passphrase')key = gpg.gen_key(input_data)print key. The Hitchhiker’s Guide to Python!

Datascience

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(): >>> dis.dis(myfunc) 2 0 LOAD_GLOBAL 0 (len) 3 LOAD_FAST 0 (alist) 6 CALL_FUNCTION 1 9 RETURN_VALUE (The “2” is a line number). The dis module defines the following functions and constants: dis.dis([bytesource]) dis.distb([tb]) Disassembles the top-of-stack function of a traceback, using the last traceback if none was passed.

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. 9.1. 9.2. Let’s begin with some definitions. Good examples of python-memcache (memcached) being used in Python?

Blogs-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. Once I week or so, I would go on a branch deletion spree by manually copying and pasting multiple branch names into a git branch -D statement. 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. Originally written in C/C++, it now provides bindings for Python. 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 Installing OpenCV Understanding the code Checking the results.