comments
stringlengths
2
31.4k
""" ======== Glossary ======== .. glossary:: along an axis Axes are defined for arrays with more than one dimension. A 2-dimensional array has two corresponding axes: the first running vertically downwards across rows (axis 0), and the second running horizontally across columns (axis 1). Many operation can take place along one of these axes. For example, we can sum each row of an array, in which case we operate along columns, or axis 1:: >>> x = np.arange(12).reshape((3,4)) >>> x array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) >>> x.sum(axis=1) array([ 6, 22, 38]) array A homogeneous container of numerical elements. Each element in the array occupies a fixed amount of memory (hence homogeneous), and can be a numerical element of a single type (such as float, int or complex) or a combination (such as ``(float, int, float)``). Each array has an associated data-type (or ``dtype``), which describes the numerical type of its elements:: >>> x = np.array([1, 2, 3], float) >>> x array([ 1., 2., 3.]) >>> x.dtype # floating point number, 64 bits of memory per element dtype('float64') # More complicated data type: each array element is a combination of # and integer and a floating point number >>> np.array([(1, 2.0), (3, 4.0)], dtype=[('x', int), ('y', float)]) array([(1, 2.0), (3, 4.0)], dtype=[('x', '<i4'), ('y', '<f8')]) Fast element-wise operations, called `ufuncs`_, operate on arrays. array_like Any sequence that can be interpreted as an ndarray. This includes nested lists, tuples, scalars and existing arrays. attribute A property of an object that can be accessed using ``obj.attribute``, e.g., ``shape`` is an attribute of an array:: >>> x = np.array([1, 2, 3]) >>> x.shape (3,) BLAS `Basic Linear Algebra Subprograms <http://en.wikipedia.org/wiki/BLAS>`_ broadcast NumPy can do operations on arrays whose shapes are mismatched:: >>> x = np.array([1, 2]) >>> y = np.array([[3], [4]]) >>> x array([1, 2]) >>> y array([[3], [4]]) >>> x + y array([[4, 5], [5, 6]]) See `doc.broadcasting`_ for more information. C order See `row-major` column-major A way to represent items in a N-dimensional array in the 1-dimensional computer memory. In column-major order, the leftmost index "varies the fastest": for example the array:: [[1, 2, 3], [4, 5, 6]] is represented in the column-major order as:: [1, 4, 2, 5, 3, 6] Column-major order is also known as the Fortran order, as the Fortran programming language uses it. decorator An operator that transforms a function. For example, a ``log`` decorator may be defined to print debugging information upon function execution:: >>> def log(f): ... def new_logging_func(*args, **kwargs): ... print "Logging call with parameters:", args, kwargs ... return f(*args, **kwargs) ... ... return new_logging_func Now, when we define a function, we can "decorate" it using ``log``:: >>> @log ... def add(a, b): ... return a + b Calling ``add`` then yields: >>> add(1, 2) Logging call with parameters: (1, 2) {} 3 dictionary Resembling a language dictionary, which provides a mapping between words and descriptions thereof, a Python dictionary is a mapping between two objects:: >>> x = {1: 'one', 'two': [1, 2]} Here, `x` is a dictionary mapping keys to values, in this case the integer 1 to the string "one", and the string "two" to the list ``[1, 2]``. The values may be accessed using their corresponding keys:: >>> x[1] 'one' >>> x['two'] [1, 2] Note that dictionaries are not stored in any specific order. Also, most mutable (see *immutable* below) objects, such as lists, may not be used as keys. For more information on dictionaries, read the `Python tutorial <http://docs.python.org/tut>`_. Fortran order See `column-major` flattened Collapsed to a one-dimensional array. See `ndarray.flatten`_ for details. immutable An object that cannot be modified after execution is called immutable. Two common examples are strings and tuples. instance A class definition gives the blueprint for constructing an object:: >>> class House(object): ... wall_colour = 'white' Yet, we have to *build* a house before it exists:: >>> h = House() # build a house Now, ``h`` is called a ``House`` instance. An instance is therefore a specific realisation of a class. iterable A sequence that allows "walking" (iterating) over items, typically using a loop such as:: >>> x = [1, 2, 3] >>> [item**2 for item in x] [1, 4, 9] It is often used in combintion with ``enumerate``:: >>> keys = ['a','b','c'] >>> for n, k in enumerate(keys): ... print "Key %d: %s" % (n, k) ... Key 0: a Key 1: b Key 2: c list A Python container that can hold any number of objects or items. The items do not have to be of the same type, and can even be lists themselves:: >>> x = [2, 2.0, "two", [2, 2.0]] The list `x` contains 4 items, each which can be accessed individually:: >>> x[2] # the string 'two' 'two' >>> x[3] # a list, containing an integer 2 and a float 2.0 [2, 2.0] It is also possible to select more than one item at a time, using *slicing*:: >>> x[0:2] # or, equivalently, x[:2] [2, 2.0] In code, arrays are often conveniently expressed as nested lists:: >>> np.array([[1, 2], [3, 4]]) array([[1, 2], [3, 4]]) For more information, read the section on lists in the `Python tutorial <http://docs.python.org/tut>`_. For a mapping type (key-value), see *dictionary*. mask A boolean array, used to select only certain elements for an operation:: >>> x = np.arange(5) >>> x array([0, 1, 2, 3, 4]) >>> mask = (x > 2) >>> mask array([False, False, False, True, True], dtype=bool) >>> x[mask] = -1 >>> x array([ 0, 1, 2, -1, -1]) masked array Array that suppressed values indicated by a mask:: >>> x = np.ma.masked_array([np.nan, 2, np.nan], [True, False, True]) >>> x masked_array(data = [-- 2.0 --], mask = [ True False True], fill_value = 1e+20) <BLANKLINE> >>> x + [1, 2, 3] masked_array(data = [-- 4.0 --], mask = [ True False True], fill_value = 1e+20) <BLANKLINE> Masked arrays are often used when operating on arrays containing missing or invalid entries. matrix A 2-dimensional ndarray that preserves its two-dimensional nature throughout operations. It has certain special operations, such as ``*`` (matrix multiplication) and ``**`` (matrix power), defined:: >>> x = np.mat([[1, 2], [3, 4]]) >>> x matrix([[1, 2], [3, 4]]) >>> x**2 matrix([[ 7, 10], [15, 22]]) method A function associated with an object. For example, each ndarray has a method called ``repeat``:: >>> x = np.array([1, 2, 3]) >>> x.repeat(2) array([1, 1, 2, 2, 3, 3]) ndarray See *array*. reference If ``a`` is a reference to ``b``, then ``(a is b) == True``. Therefore, ``a`` and ``b`` are different names for the same Python object. row-major A way to represent items in a N-dimensional array in the 1-dimensional computer memory. In row-major order, the rightmost index "varies the fastest": for example the array:: [[1, 2, 3], [4, 5, 6]] is represented in the row-major order as:: [1, 2, 3, 4, 5, 6] Row-major order is also known as the C order, as the C programming language uses it. New Numpy arrays are by default in row-major order. self Often seen in method signatures, ``self`` refers to the instance of the associated class. For example: >>> class Paintbrush(object): ... color = 'blue' ... ... def paint(self): ... print "Painting the city %s!" % self.color ... >>> p = Paintbrush() >>> p.color = 'red' >>> p.paint() # self refers to 'p' Painting the city red! slice Used to select only certain elements from a sequence:: >>> x = range(5) >>> x [0, 1, 2, 3, 4] >>> x[1:3] # slice from 1 to 3 (excluding 3 itself) [1, 2] >>> x[1:5:2] # slice from 1 to 5, but skipping every second element [1, 3] >>> x[::-1] # slice a sequence in reverse [4, 3, 2, 1, 0] Arrays may have more than one dimension, each which can be sliced individually:: >>> x = np.array([[1, 2], [3, 4]]) >>> x array([[1, 2], [3, 4]]) >>> x[:, 1] array([2, 4]) tuple A sequence that may contain a variable number of types of any kind. A tuple is immutable, i.e., once constructed it cannot be changed. Similar to a list, it can be indexed and sliced:: >>> x = (1, 'one', [1, 2]) >>> x (1, 'one', [1, 2]) >>> x[0] 1 >>> x[:2] (1, 'one') A useful concept is "tuple unpacking", which allows variables to be assigned to the contents of a tuple:: >>> x, y = (1, 2) >>> x, y = 1, 2 This is often used when a function returns multiple values: >>> def return_many(): ... return 1, 'alpha', None >>> a, b, c = return_many() >>> a, b, c (1, 'alpha', None) >>> a 1 >>> b 'alpha' ufunc Universal function. A fast element-wise array operation. Examples include ``add``, ``sin`` and ``logical_or``. view An array that does not own its data, but refers to another array's data instead. For example, we may create a view that only shows every second element of another array:: >>> x = np.arange(5) >>> x array([0, 1, 2, 3, 4]) >>> y = x[::2] >>> y array([0, 2, 4]) >>> x[0] = 3 # changing x changes y as well, since y is a view on x >>> y array([3, 2, 4]) wrapper Python is a high-level (highly abstracted, or English-like) language. This abstraction comes at a price in execution speed, and sometimes it becomes necessary to use lower level languages to do fast computations. A wrapper is code that provides a bridge between high and the low level languages, allowing, e.g., Python to execute code written in C or Fortran. Examples include ctypes, SWIG and Cython (which wraps C and C++) and f2py (which wraps Fortran). """
""" Limits ====== Implemented according to the PhD thesis http://www.cybertester.com/data/gruntz.pdf, which contains very thorough descriptions of the algorithm including many examples. We summarize here the gist of it. All functions are sorted according to how rapidly varying they are at infinity using the following rules. Any two functions f and g can be compared using the properties of L: L=lim log|f(x)| / log|g(x)| (for x -> oo) We define >, < ~ according to:: 1. f > g .... L=+-oo we say that: - f is greater than any power of g - f is more rapidly varying than g - f goes to infinity/zero faster than g 2. f < g .... L=0 we say that: - f is lower than any power of g 3. f ~ g .... L!=0, +-oo we say that: - both f and g are bounded from above and below by suitable integral powers of the other Examples ======== :: 2 < x < exp(x) < exp(x**2) < exp(exp(x)) 2 ~ 3 ~ -5 x ~ x**2 ~ x**3 ~ 1/x ~ x**m ~ -x exp(x) ~ exp(-x) ~ exp(2x) ~ exp(x)**2 ~ exp(x+exp(-x)) f ~ 1/f So we can divide all the functions into comparability classes (x and x^2 belong to one class, exp(x) and exp(-x) belong to some other class). In principle, we could compare any two functions, but in our algorithm, we don't compare anything below the class 2~3~-5 (for example log(x) is below this), so we set 2~3~-5 as the lowest comparability class. Given the function f, we find the list of most rapidly varying (mrv set) subexpressions of it. This list belongs to the same comparability class. Let's say it is {exp(x), exp(2x)}. Using the rule f ~ 1/f we find an element "w" (either from the list or a new one) from the same comparability class which goes to zero at infinity. In our example we set w=exp(-x) (but we could also set w=exp(-2x) or w=exp(-3x) ...). We rewrite the mrv set using w, in our case {1/w, 1/w^2}, and substitute it into f. Then we expand f into a series in w:: f = c0*w^e0 + c1*w^e1 + ... + O(w^en), where e0<e1<...<en, c0!=0 but for x->oo, lim f = lim c0*w^e0, because all the other terms go to zero, because w goes to zero faster than the ci and ei. So:: for e0>0, lim f = 0 for e0<0, lim f = +-oo (the sign depends on the sign of c0) for e0=0, lim f = lim c0 We need to recursively compute limits at several places of the algorithm, but as is shown in the PhD thesis, it always finishes. Important functions from the implementation: compare(a, b, x) compares "a" and "b" by computing the limit L. mrv(e, x) returns list of most rapidly varying (mrv) subexpressions of "e" rewrite(e, Omega, x, wsym) rewrites "e" in terms of w leadterm(f, x) returns the lowest power term in the series of f mrv_leadterm(e, x) returns the lead term (c0, e0) for e limitinf(e, x) computes lim e (for x->oo) limit(e, z, z0) computes any limit by converting it to the case x->oo All the functions are really simple and straightforward except rewrite(), which is the most difficult/complex part of the algorithm. When the algorithm fails, the bugs are usually in the series expansion (i.e. in SymPy) or in rewrite. This code is almost exact rewrite of the Maple code inside the Gruntz thesis. Debugging --------- Because the gruntz algorithm is highly recursive, it's difficult to figure out what went wrong inside a debugger. Instead, turn on nice debug prints by defining the environment variable SYMPY_DEBUG. For example: [user@localhost]: SYMPY_DEBUG=True ./bin/isympy In [1]: limit(sin(x)/x, x, 0) limitinf(_x*sin(1/_x), _x) = 1 +-mrv_leadterm(_x*sin(1/_x), _x) = (1, 0) | +-mrv(_x*sin(1/_x), _x) = set([_x]) | | +-mrv(_x, _x) = set([_x]) | | +-mrv(sin(1/_x), _x) = set([_x]) | | +-mrv(1/_x, _x) = set([_x]) | | +-mrv(_x, _x) = set([_x]) | +-mrv_leadterm(exp(_x)*sin(exp(-_x)), _x, set([exp(_x)])) = (1, 0) | +-rewrite(exp(_x)*sin(exp(-_x)), set([exp(_x)]), _x, _w) = (1/_w*sin(_w), -_x) | +-sign(_x, _x) = 1 | +-mrv_leadterm(1, _x) = (1, 0) +-sign(0, _x) = 0 +-limitinf(1, _x) = 1 And check manually which line is wrong. Then go to the source code and debug this function to figure out the exact problem. """
""" # ggame The simple cross-platform sprite and game platform for Brython Server (Pygame, Tkinter to follow?). Ggame stands for a couple of things: "good game" (of course!) and also "git game" or "github game" because it is designed to operate with [Brython Server](http://runpython.com) in concert with Github as a backend file store. Ggame is **not** intended to be a full-featured gaming API, with every bell and whistle. Ggame is designed primarily as a tool for teaching computer programming, recognizing that the ability to create engaging and interactive games is a powerful motivator for many progamming students. Accordingly, any functional or performance enhancements that *can* be reasonably implemented by the user are left as an exercise. ## Functionality Goals The ggame library is intended to be trivially easy to use. For example: from ggame import App, ImageAsset, Sprite # Create a displayed object at 100,100 using an image asset Sprite(ImageAsset("ggame/bunny.png"), (100,100)) # Create the app, with a 500x500 pixel stage app = App(500,500) # Run the app app.run() ## Overview There are three major components to the `ggame` system: Assets, Sprites and the App. ### Assets Asset objects (i.e. `ggame.ImageAsset`, etc.) typically represent separate files that are provided by the "art department". These might be background images, user interface images, or images that represent objects in the game. In addition, `ggame.SoundAsset` is used to represent sound files (`.wav` or `.mp3` format) that can be played in the game. Ggame also extends the asset concept to include graphics that are generated dynamically at run-time, such as geometrical objects, e.g. rectangles, lines, etc. ### Sprites All of the visual aspects of the game are represented by instances of `ggame.Sprite` or subclasses of it. ### App Every ggame application must create a single instance of the `ggame.App` class (or a sub-class of it). Creating an instance of the `ggame.App` class will initiate creation of a pop-up window on your browser. Executing the app's `run` method will begin the process of refreshing the visual assets on the screen. ### Events No game is complete without a player and players produce events. Your code handles user input by registering to receive keyboard and mouse events using `ggame.App.listenKeyEvent` and `ggame.App.listenMouseEvent` methods. ## Execution Environment Ggame is designed to be executed in a web browser using [Brython](http://brython.info/), [Pixi.js](http://www.pixijs.com/) and [Buzz](http://buzz.jaysalvat.com/). The easiest way to do this is by executing from [runpython](http://runpython.com), with source code residing on [github](http://github.com). When using [runpython](http://runpython.com), you will have to configure your browser to allow popup windows. To use Ggame in your own application, you will minimally need to create a folder called `ggame` in your project. Within `ggame`, copy the `ggame.py`, `sysdeps.py` and `__init__.py` files from the [ggame project](https://github.com/BrythonServer/ggame). ### Include Ggame as a Git Subtree From the same directory as your own python sources (note: you must have an existing git repository with committed files in order for the following to work properly), execute the following terminal commands: git remote add -f ggame https://github.com/BrythonServer/ggame.git git merge -s ours --no-commit ggame/master mkdir ggame git read-tree --prefix=ggame/ -u ggame/master git commit -m "Merge ggame project as our subdirectory" If you want to pull in updates from ggame in the future: git pull -s subtree ggame master You can see an example of how a ggame subtree is used by examining the [Brython Server Spacewar](https://github.com/BrythonServer/Spacewar) repo on Github. ## Geometry When referring to screen coordinates, note that the x-axis of the computer screen is *horizontal* with the zero position on the left hand side of the screen. The y-axis is *vertical* with the zero position at the **top** of the screen. Increasing positive y-coordinates correspond to the downward direction on the computer screen. Note that this is **different** from the way you may have learned about x and y coordinates in math class! """
# Check the basic discovery process, including a sub-suite. # # RUN: %{lit} %{inputs}/discovery \ # RUN: -j 1 --debug --show-tests --show-suites \ # RUN: -v > %t.out 2> %t.err # RUN: FileCheck --check-prefix=CHECK-BASIC-OUT < %t.out %s # RUN: FileCheck --check-prefix=CHECK-BASIC-ERR < %t.err %s # # CHECK-BASIC-ERR: loading suite config '{{.*}}/discovery/lit.cfg' # CHECK-BASIC-ERR: loading local config '{{.*}}/discovery/subdir/lit.local.cfg' # CHECK-BASIC-ERR: loading suite config '{{.*}}/discovery/subsuite/lit.cfg' # # CHECK-BASIC-OUT: -- Test Suites -- # CHECK-BASIC-OUT: sub-suite - 2 tests # CHECK-BASIC-OUT: Source Root: {{.*/discovery/subsuite$}} # CHECK-BASIC-OUT: Exec Root : {{.*/discovery/subsuite$}} # CHECK-BASIC-OUT: top-level-suite - 3 tests # CHECK-BASIC-OUT: Source Root: {{.*/discovery$}} # CHECK-BASIC-OUT: Exec Root : {{.*/discovery$}} # # CHECK-BASIC-OUT: -- Available Tests -- # CHECK-BASIC-OUT: sub-suite :: test-one # CHECK-BASIC-OUT: sub-suite :: test-two # CHECK-BASIC-OUT: top-level-suite :: subdir/test-three # CHECK-BASIC-OUT: top-level-suite :: test-one # CHECK-BASIC-OUT: top-level-suite :: test-two # Check discovery when exact test names are given. # # RUN: %{lit} \ # RUN: %{inputs}/discovery/subdir/test-three.py \ # RUN: %{inputs}/discovery/subsuite/test-one.txt \ # RUN: -j 1 --show-tests --show-suites -v > %t.out # RUN: FileCheck --check-prefix=CHECK-EXACT-TEST < %t.out %s # # CHECK-EXACT-TEST: -- Available Tests -- # CHECK-EXACT-TEST: sub-suite :: test-one # CHECK-EXACT-TEST: top-level-suite :: subdir/test-three # Check discovery when using an exec path. # # RUN: %{lit} %{inputs}/exec-discovery \ # RUN: -j 1 --debug --show-tests --show-suites \ # RUN: -v > %t.out 2> %t.err # RUN: FileCheck --check-prefix=CHECK-ASEXEC-OUT < %t.out %s # RUN: FileCheck --check-prefix=CHECK-ASEXEC-ERR < %t.err %s # # CHECK-ASEXEC-ERR: loading suite config '{{.*}}/exec-discovery/lit.site.cfg' # CHECK-ASEXEC-ERR: load_config from '{{.*}}/discovery/lit.cfg' # CHECK-ASEXEC-ERR: loaded config '{{.*}}/discovery/lit.cfg' # CHECK-ASEXEC-ERR: loaded config '{{.*}}/exec-discovery/lit.site.cfg' # CHECK-ASEXEC-ERR: loading local config '{{.*}}/discovery/subdir/lit.local.cfg' # CHECK-ASEXEC-ERR: loading suite config '{{.*}}/discovery/subsuite/lit.cfg' # # CHECK-ASEXEC-OUT: -- Test Suites -- # CHECK-ASEXEC-OUT: sub-suite - 2 tests # CHECK-ASEXEC-OUT: Source Root: {{.*/discovery/subsuite$}} # CHECK-ASEXEC-OUT: Exec Root : {{.*/discovery/subsuite$}} # CHECK-ASEXEC-OUT: top-level-suite - 3 tests # CHECK-ASEXEC-OUT: Source Root: {{.*/discovery$}} # CHECK-ASEXEC-OUT: Exec Root : {{.*/exec-discovery$}} # # CHECK-ASEXEC-OUT: -- Available Tests -- # CHECK-ASEXEC-OUT: sub-suite :: test-one # CHECK-ASEXEC-OUT: sub-suite :: test-two # CHECK-ASEXEC-OUT: top-level-suite :: subdir/test-three # CHECK-ASEXEC-OUT: top-level-suite :: test-one # CHECK-ASEXEC-OUT: top-level-suite :: test-two # Check discovery when exact test names are given. # # FIXME: Note that using a path into a subsuite doesn't work correctly here. # # RUN: %{lit} \ # RUN: %{inputs}/exec-discovery/subdir/test-three.py \ # RUN: -j 1 --show-tests --show-suites -v > %t.out # RUN: FileCheck --check-prefix=CHECK-ASEXEC-EXACT-TEST < %t.out %s # # CHECK-ASEXEC-EXACT-TEST: -- Available Tests -- # CHECK-ASEXEC-EXACT-TEST: top-level-suite :: subdir/test-three # Check that we don't recurse infinitely when loading an site specific test # suite located inside the test source root. # # RUN: %{lit} \ # RUN: %{inputs}/exec-discovery-in-tree/obj/ \ # RUN: -j 1 --show-tests --show-suites -v > %t.out # RUN: FileCheck --check-prefix=CHECK-ASEXEC-INTREE < %t.out %s # # CHECK-ASEXEC-INTREE: exec-discovery-in-tree-suite - 1 tests # CHECK-ASEXEC-INTREE-NEXT: Source Root: {{.*/exec-discovery-in-tree$}} # CHECK-ASEXEC-INTREE-NEXT: Exec Root : {{.*/exec-discovery-in-tree/obj$}} # CHECK-ASEXEC-INTREE-NEXT: -- Available Tests -- # CHECK-ASEXEC-INTREE-NEXT: exec-discovery-in-tree-suite :: test-one
# This code is part of Ansible, but is an independent component. # This particular file snippet, and this file snippet only, is BSD licensed. # Modules you write using this snippet, which is embedded dynamically by Ansible # still belong to the author of the module, and may assign their own license # to the complete work. # # Copyright (c), NAME <EMAIL>, 2012-2013 # Copyright (c), NAME <EMAIL>, 2015 # All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, # are permitted provided that the following conditions are met: # # * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. # IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # # The match_hostname function and supporting code is under the terms and # conditions of the Python Software Foundation License. They were taken from # the Python3 standard library and adapted for use in Python2. See comments in the # source for which code precisely is under this License. PSF License text # follows: # # PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2 # -------------------------------------------- # # 1. This LICENSE AGREEMENT is between the Python Software Foundation # ("PSF"), and the Individual or Organization ("Licensee") accessing and # otherwise using this software ("Python") in source or binary form and # its associated documentation. # # 2. Subject to the terms and conditions of this License Agreement, PSF hereby # grants Licensee a nonexclusive, royalty-free, world-wide license to reproduce, # analyze, test, perform and/or display publicly, prepare derivative works, # distribute, and otherwise use Python alone or in any derivative version, # provided, however, that PSF's License Agreement and PSF's notice of copyright, # i.e., "Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, # 2011, 2012, 2013, 2014 Python Software Foundation; All Rights Reserved" are # retained in Python alone or in any derivative version prepared by Licensee. # # 3. In the event Licensee prepares a derivative work that is based on # or incorporates Python or any part thereof, and wants to make # the derivative work available to others as provided herein, then # Licensee hereby agrees to include in any such work a brief summary of # the changes made to Python. # # 4. PSF is making Python available to Licensee on an "AS IS" # basis. PSF MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR # IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, PSF MAKES NO AND # DISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS # FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF PYTHON WILL NOT # INFRINGE ANY THIRD PARTY RIGHTS. # # 5. PSF SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON # FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS # A RESULT OF MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON, # OR ANY DERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF. # # 6. This License Agreement will automatically terminate upon a material # breach of its terms and conditions. # # 7. Nothing in this License Agreement shall be deemed to create any # relationship of agency, partnership, or joint venture between PSF and # Licensee. This License Agreement does not grant permission to use PSF # trademarks or trade name in a trademark sense to endorse or promote # products or services of Licensee, or any third party. # # 8. By copying, installing or otherwise using Python, Licensee # agrees to be bound by the terms and conditions of this License # Agreement.
""" =================== Universal Functions =================== Ufuncs are, generally speaking, mathematical functions or operations that are applied element-by-element to the contents of an array. That is, the result in each output array element only depends on the value in the corresponding input array (or arrays) and on no other array elements. NumPy comes with a large suite of ufuncs, and scipy extends that suite substantially. The simplest example is the addition operator: :: >>> np.array([0,2,3,4]) + np.array([1,1,-1,2]) array([1, 3, 2, 6]) The unfunc module lists all the available ufuncs in numpy. Documentation on the specific ufuncs may be found in those modules. This documentation is intended to address the more general aspects of unfuncs common to most of them. All of the ufuncs that make use of Python operators (e.g., +, -, etc.) have equivalent functions defined (e.g. add() for +) Type coercion ============= What happens when a binary operator (e.g., +,-,\\*,/, etc) deals with arrays of two different types? What is the type of the result? Typically, the result is the higher of the two types. For example: :: float32 + float64 -> float64 int8 + int32 -> int32 int16 + float32 -> float32 float32 + complex64 -> complex64 There are some less obvious cases generally involving mixes of types (e.g. uints, ints and floats) where equal bit sizes for each are not capable of saving all the information in a different type of equivalent bit size. Some examples are int32 vs float32 or uint32 vs int32. Generally, the result is the higher type of larger size than both (if available). So: :: int32 + float32 -> float64 uint32 + int32 -> int64 Finally, the type coercion behavior when expressions involve Python scalars is different than that seen for arrays. Since Python has a limited number of types, combining a Python int with a dtype=np.int8 array does not coerce to the higher type but instead, the type of the array prevails. So the rules for Python scalars combined with arrays is that the result will be that of the array equivalent the Python scalar if the Python scalar is of a higher 'kind' than the array (e.g., float vs. int), otherwise the resultant type will be that of the array. For example: :: Python int + int8 -> int8 Python float + int8 -> float64 ufunc methods ============= Binary ufuncs support 4 methods. **.reduce(arr)** applies the binary operator to elements of the array in sequence. For example: :: >>> np.add.reduce(np.arange(10)) # adds all elements of array 45 For multidimensional arrays, the first dimension is reduced by default: :: >>> np.add.reduce(np.arange(10).reshape(2,5)) array([ 5, 7, 9, 11, 13]) The axis keyword can be used to specify different axes to reduce: :: >>> np.add.reduce(np.arange(10).reshape(2,5),axis=1) array([10, 35]) **.accumulate(arr)** applies the binary operator and generates an an equivalently shaped array that includes the accumulated amount for each element of the array. A couple examples: :: >>> np.add.accumulate(np.arange(10)) array([ 0, 1, 3, 6, 10, 15, 21, 28, 36, 45]) >>> np.multiply.accumulate(np.arange(1,9)) array([ 1, 2, 6, 24, 120, 720, 5040, 40320]) The behavior for multidimensional arrays is the same as for .reduce(), as is the use of the axis keyword). **.reduceat(arr,indices)** allows one to apply reduce to selected parts of an array. It is a difficult method to understand. See the documentation at: **.outer(arr1,arr2)** generates an outer operation on the two arrays arr1 and arr2. It will work on multidimensional arrays (the shape of the result is the concatenation of the two input shapes.: :: >>> np.multiply.outer(np.arange(3),np.arange(4)) array([[0, 0, 0, 0], [0, 1, 2, 3], [0, 2, 4, 6]]) Output arguments ================ All ufuncs accept an optional output array. The array must be of the expected output shape. Beware that if the type of the output array is of a different (and lower) type than the output result, the results may be silently truncated or otherwise corrupted in the downcast to the lower type. This usage is useful when one wants to avoid creating large temporary arrays and instead allows one to reuse the same array memory repeatedly (at the expense of not being able to use more convenient operator notation in expressions). Note that when the output argument is used, the ufunc still returns a reference to the result. >>> x = np.arange(2) >>> np.add(np.arange(2),np.arange(2.),x) array([0, 2]) >>> x array([0, 2]) and & or as ufuncs ================== Invariably people try to use the python 'and' and 'or' as logical operators (and quite understandably). But these operators do not behave as normal operators since Python treats these quite differently. They cannot be overloaded with array equivalents. Thus using 'and' or 'or' with an array results in an error. There are two alternatives: 1) use the ufunc functions logical_and() and logical_or(). 2) use the bitwise operators & and \\|. The drawback of these is that if the arguments to these operators are not boolean arrays, the result is likely incorrect. On the other hand, most usages of logical_and and logical_or are with boolean arrays. As long as one is careful, this is a convenient way to apply these operators. """
""" The react module provides functionality for Reactive Programming (RP) and Functional Reactive Programming (FRP). It is a bit difficult to explain what FRP really is. This is because every implementation has its own take on it, and because it requires a bit of a paradigm shift compared to classic event-driven programming. FRP does not have to be difficult and we think our implementation of ``flexx.react`` is relatively easy to use. This brief guide takes you through some of the FRP aspects using code examples. What is FRP ----------- (Don't worry if the next two paragraphs sound complicated; things should start to make sense when we explain thing using code.) *Where event-driven programming is about reacting to things that happen, RP is about staying up to date with changing signals.* In RP the different components in an application communicate via streams of data. In other words, components keep track of (and react to) the *signal values* of other components. All signals (except source/input signals) have one or more upstream signals, and can combine and or modify these to produce a new signal value. The value of each signal is *cached*, so that the operations applied to the signal values only have to be performed when any upstream signal has changed. When a signal changes its value, it will *notify* its downstream signals, so that everything stays up-to-date. In ``flexx.react`` signals are addressed using a string. This may seem unusual at first, but it allows easy binding for signals on classes, allow signal loops, and has other advantages that we'll discuss when we talk about dynamism. Signals ------- A signal can be created by decorating a function. In RP-speak, the function is "lifted" to a signal: .. code-block:: py # The function greet() is used to react to signal "name" @react.connect('name') def greet(n): print('hello %!' % n) The example above looks quite similar to how some event-drive applications allow binding callbacks to events. There are, however, a few differences: a) The greet function has now become a signal object, which has an output of its own (although the output is None in this case, because the function does not return a value, more on that below); b) The function (which we'd call the "callback" in an event driven system) does not accept an event object, but a value that corresponds to the upstream signal value. One other advantage of a RP system is that signals can *connect to multiple upsteam signals*: .. code-block:: py @react.connect('first_name', 'last_name') def greet(first, last): print('hello %s %s!' % (first, last) This is a feature that saves a lot of overhead. For any "callback" that you define, you specify *exactly* what input signals there are, and it will always be up to date. Doing that in an event-driven system quickly results in a spaghetti of callbacks and boilerplate to keep track of state. The function of a signal gets called directly when any of the upstream signals (or the upstream-upstream signals) change. The return value of the function represents the output signal value, which can also be None. When the return value is ``undefined`` (from ``react.undefined`` or ``pyscript.undefined``), the value is ignored and the signal maintains its current value. Source and input signals ------------------------ Signals must start somewhere. The *source signal* has a ``_set()`` method that the programmer can use to set the value of the signal: .. code-block:: py @react.source def name(n): return n The function for this source signal is very simple. You usually want to do some input checking and/or normalization here. Especialy if the input comes from the user, as is the case with the input signal. The *input signal* is a source signal that can be called with an argument to set its value: .. code-block:: py @react.input def name(n='john NAME if not isinstance(n, str): raise ValueError('Name must be a string') return n.capitalized() # And later ... name('jane NAME can also see how the default value of the function argument can be used to specify the initial signal value. Source and input signals generally do not have upstream signals, but they can have them. A complete example ------------------ .. code-block:: py @react.input def first_name(s='john'): return str(s) @react.input def last_name(s='NAME return str(s) @react.connect('first_name', 'last_name') def full_name(first, 'last'): return '%s %s' % (first, last) @react.connect('full_name') def greet(name): print('hello %s!' % name) Lazy signals ------------ In contrast to normal signals, a *lazy signal* does not update immediately when the upstream signals changes. It is updated automatically (lazily) whenever its value is queried. Note that this has little effect when there is a normal signal downstream. Lazy signals can be convenient in a situation where values changes rapidly, while the current value is only needed sparingly. To create, use the ``lazy()`` decorator: .. code-block:: py @react.lazy('first_name', 'last_name') def full_name(first, last): return '%s %s' % (first, last) Caching ------- .. code-block:: py @react.input def data_select(id): return str(id) @react.input def data_clean(clean): return bool(clean) @react.connect('data_select') def data(id): open_connection(id) return get_data_from_the_web() # this may take a while @react.connect('data', 'data_clean') def show_data(data, clean): if clean: data = clean_func(data) plotter.show(data) This hypothetical example shows how caching helps keep apps efficient. The ``data`` signal will only update when the ``data_select`` changes. When ``data_clean`` is changes, the ``show_data`` signal updates, but it will use the cached value of the data. The HasSignals class -------------------- It is often convenient to create classes that have signals. To do so, inherit from the ``HasSignals`` class: .. code-block:: py class Person(react.HasSignals): def __init__(self, father): assert isinstance(father, Person) self.father = father react.HasSignals.__init__(self) @react.input def first_name(s): return s @react.connect('father.last_name') def last_name(s): return s @react.connect('first_name', 'last_name') de greet(first, last): print('hello %s %s!' % (first, last)) The above example show how you can directly refer to signals on the object using their name, and even use dot notation to address the signal of an attribute of the object. It also shows that the signal functions do not have a ``self`` argument. They do not have to, but they can if they needs access to the instance. Dynamism -------- With dynamism, you can refer to signals of signals, and have the signal connections be made automatically. Let's modify the last example a bit: .. code-block:: py class Person(react.HasSignals): def __init__(self, father): self.father(father) react.HasSignals.__init__(self) @react.input def father(f): assert isinstance(f, Person) return f @react.connect('father.last_name') def last_name(s): return s ... In this case, the last name of the father will change when either the father changes, or the father changes its name. Dynamism also supports star notation: .. code-block:: py class Person(react.HasSignals): @react.input def children(cc): assert isinstance(cc, tuple) assert all([isinstance(c, Person) for c in cc]) return cc @react.connect('children.*') def child_names(*names): return ', '.join(name) Signal history -------------- The signal object provides a bit more information than only its value. The most notable is the value of the signal before the last change. .. code-block:: py class Person(react.HasSignals): @react.connect('first_name'): def react_to_name_change(self, new_name): old_name = self.first_name.last_value new_name = self.first_name.value # == new_name The signal value also holds information on value update times, but this is currently private. We'll have to see if this is reliable and convenient enough to make it public. Functional RP ------------- The "F" in FRP stands for functional. Currently, there is limited support for that, for example: .. code-block:: py filter = lambda x: x>0 @react.connect(react.filter(filter, 'number')) def show_positive_numbers(v): print(v) This functionality is to be extended in the future. Some things just are events --------------------------- Many things can be described as changing signal values. Even "left_mouse_down" works pretty well. However, some things really *are* events, like key presses and timers. How to handle these is still something we'd need to work out ... """
# # XML-RPC CLIENT LIBRARY # $Id$ # # an XML-RPC client interface for Python. # # the marshalling and response parser code can also be used to # implement XML-RPC servers. # # Notes: # this version is designed to work with Python 2.1 or newer. # # History: # 1999-01-14 fl Created # 1999-01-15 fl Changed dateTime to use localtime # 1999-01-16 fl Added Binary/base64 element, default to RPC2 service # 1999-01-19 fl Fixed array data element (from Skip Montanaro) # 1999-01-21 fl Fixed dateTime constructor, etc. # 1999-02-02 fl Added fault handling, handle empty sequences, etc. # 1999-02-10 fl Fixed problem with empty responses (from Skip Montanaro) # 1999-06-20 fl Speed improvements, pluggable parsers/transports (0.9.8) # 2000-11-28 fl Changed boolean to check the truth value of its argument # 2001-02-24 fl Added encoding/Unicode/SafeTransport patches # 2001-02-26 fl Added compare support to wrappers (0.9.9/1.0b1) # 2001-03-28 fl Make sure response tuple is a singleton # 2001-03-29 fl Don't require empty params element (from NAME 2001-06-10 fl Folded in _xmlrpclib accelerator support (1.0b2) # 2001-08-20 fl Base xmlrpclib.Error on built-in Exception (from NAME 2001-09-03 fl Allow Transport subclass to override getparser # 2001-09-10 fl Lazy import of urllib, cgi, xmllib (20x import speedup) # 2001-10-01 fl Remove containers from memo cache when done with them # 2001-10-01 fl Use faster escape method (80% dumps speedup) # 2001-10-02 fl More dumps microtuning # 2001-10-04 fl Make sure import expat gets a parser (from NAME 2001-10-10 sm Allow long ints to be passed as ints if they don't overflow # 2001-10-17 sm Test for int and long overflow (allows use on 64-bit systems) # 2001-11-12 fl Use repr() to marshal doubles (from NAME 2002-03-17 fl Avoid buffered read when possible (from NAME 2002-04-07 fl Added pythondoc comments # 2002-04-16 fl Added __str__ methods to datetime/binary wrappers # 2002-05-15 fl Added error constants (from NAME 2002-06-27 fl Merged with Python CVS version # 2002-10-22 fl Added basic authentication (based on code from NAME 2003-01-22 sm Add support for the bool type # 2003-02-27 gvr Remove apply calls # 2003-04-24 sm Use cStringIO if available # 2003-04-25 ak Add support for nil # 2003-06-15 gn Add support for time.struct_time # 2003-07-12 gp Correct marshalling of Faults # 2003-10-31 mvl Add multicall support # 2004-08-20 mvl Bump minimum supported Python version to 2.1 # 2014-12-02 ch/doko Add workaround for gzip bomb vulnerability # # Copyright (c) 1999-2002 by Secret Labs AB. # Copyright (c) 1999-2002 by NAME Lundh. # # EMAIL http://www.pythonware.com # # -------------------------------------------------------------------- # The XML-RPC client interface is # # Copyright (c) 1999-2002 by Secret Labs AB # Copyright (c) 1999-2002 by NAME Lundh # # By obtaining, using, and/or copying this software and/or its # associated documentation, you agree that you have read, understood, # and will comply with the following terms and conditions: # # Permission to use, copy, modify, and distribute this software and # its associated documentation for any purpose and without fee is # hereby granted, provided that the above copyright notice appears in # all copies, and that both that copyright notice and this permission # notice appear in supporting documentation, and that the name of # Secret Labs AB or the author not be used in advertising or publicity # pertaining to distribution of the software without specific, written # prior permission. # # SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD # TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT- # ABILITY AND FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE # OF THIS SOFTWARE. # --------------------------------------------------------------------
# # XML-RPC CLIENT LIBRARY # $Id$ # # an XML-RPC client interface for Python. # # the marshalling and response parser code can also be used to # implement XML-RPC servers. # # Notes: # this version is designed to work with Python 2.1 or newer. # # History: # 1999-01-14 fl Created # 1999-01-15 fl Changed dateTime to use localtime # 1999-01-16 fl Added Binary/base64 element, default to RPC2 service # 1999-01-19 fl Fixed array data element (from Skip Montanaro) # 1999-01-21 fl Fixed dateTime constructor, etc. # 1999-02-02 fl Added fault handling, handle empty sequences, etc. # 1999-02-10 fl Fixed problem with empty responses (from Skip Montanaro) # 1999-06-20 fl Speed improvements, pluggable parsers/transports (0.9.8) # 2000-11-28 fl Changed boolean to check the truth value of its argument # 2001-02-24 fl Added encoding/Unicode/SafeTransport patches # 2001-02-26 fl Added compare support to wrappers (0.9.9/1.0b1) # 2001-03-28 fl Make sure response tuple is a singleton # 2001-03-29 fl Don't require empty params element (from NAME 2001-06-10 fl Folded in _xmlrpclib accelerator support (1.0b2) # 2001-08-20 fl Base xmlrpclib.Error on built-in Exception (from NAME 2001-09-03 fl Allow Transport subclass to override getparser # 2001-09-10 fl Lazy import of urllib, cgi, xmllib (20x import speedup) # 2001-10-01 fl Remove containers from memo cache when done with them # 2001-10-01 fl Use faster escape method (80% dumps speedup) # 2001-10-02 fl More dumps microtuning # 2001-10-04 fl Make sure import expat gets a parser (from NAME 2001-10-10 sm Allow long ints to be passed as ints if they don't overflow # 2001-10-17 sm Test for int and long overflow (allows use on 64-bit systems) # 2001-11-12 fl Use repr() to marshal doubles (from NAME 2002-03-17 fl Avoid buffered read when possible (from NAME 2002-04-07 fl Added pythondoc comments # 2002-04-16 fl Added __str__ methods to datetime/binary wrappers # 2002-05-15 fl Added error constants (from NAME 2002-06-27 fl Merged with Python CVS version # 2002-10-22 fl Added basic authentication (based on code from NAME 2003-01-22 sm Add support for the bool type # 2003-02-27 gvr Remove apply calls # 2003-04-24 sm Use cStringIO if available # 2003-04-25 ak Add support for nil # 2003-06-15 gn Add support for time.struct_time # 2003-07-12 gp Correct marshalling of Faults # 2003-10-31 mvl Add multicall support # 2004-08-20 mvl Bump minimum supported Python version to 2.1 # # Copyright (c) 1999-2002 by Secret Labs AB. # Copyright (c) 1999-2002 by NAME EMAIL http://www.pythonware.com # # -------------------------------------------------------------------- # The XML-RPC client interface is # # Copyright (c) 1999-2002 by Secret Labs AB # Copyright (c) 1999-2002 by NAME By obtaining, using, and/or copying this software and/or its # associated documentation, you agree that you have read, understood, # and will comply with the following terms and conditions: # # Permission to use, copy, modify, and distribute this software and # its associated documentation for any purpose and without fee is # hereby granted, provided that the above copyright notice appears in # all copies, and that both that copyright notice and this permission # notice appear in supporting documentation, and that the name of # Secret Labs AB or the author not be used in advertising or publicity # pertaining to distribution of the software without specific, written # prior permission. # # SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD # TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT- # ABILITY AND FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE # OF THIS SOFTWARE. # -------------------------------------------------------------------- # # things to look into some day: # TODO: sort out True/False/boolean issues for Python 2.3
######### # Notes # ######### # # Archive.org won't accept wget user agent string---it can be anything else # # When fetching source files from arxiv.org: # 0408420 gives most recent # 0408420vN gives version N # 0408420vN if N > number of versions gives most recent version # (the behavior is same for old and new archive identifiers) # # The conventions for storing latex source archives files are taken # from those used in the bulk data made available by arxiv.org via # Amazon.com's S3 service. This makes it possible to download a bunch # of source files, untar them, and start working on the data without # pre-processing the files in any way. # # Information here: http://arxiv.org/help/bulk_data_s3 # # Command to bulk download arxiv sources to the current directory # s3cmd sync --add-header="x-amz-request-payer: requester" s3://arxiv/src/ . # # The entire archive is divided into sub-directories by YYMM. Files # in each directory can be pdf files or gzip files. The gunzipped # file can be any number of things: latex source, a tar file, # postscript, html, plain text, (and more?). It might have been nice # if the pdf files were gzipped so that _everything_ could be handled # the same way (gunzip, figure out what it is, act appropriately), but # the arxiv.org people have decided not to do that. It's true that # PDF files are already compressed, so gzipping them is kind of a # waste. # # However, when you download a paper via # http://arxiv.org/e-print/identifier, you don't know if the file will # be a PDF or a gzip file, and the resulting file has no extension. # Therefore when fetching papers (from the arxiv.org RSS feed, for # instance), the code below looks at the file type and puts the # appropriate extension so that it goes into the data directory just # like the bulk data provided by arxiv.org via S3. # # Thus when you ask for the name of the data file associated with an # arxiv id, you may want it without an extension (if it was downloaded # via wget and you want to put the appropriate extension on), or you # may want it _with_ the extension (in which case the code must look # to see if it's a pdf or gz file, and give you the correct filename). # # Note that it's necessary to call 'file' on the gunzipped file. # Calling 'file' on the gzipped file allows you to figure out what's # inside most of the time, but sometimes the results are hilariously # wrong. All of these contain valid latex: # 1211.0074.gz: Minix filesystem, V2, 51878 zones # cond-mat0701210.gz: gzip compressed data, was "/data/new/0022/0022418/src/with", # math0701257.gz: x86 boot sector, code offset 0x8b # # Even calling file on the gunzipped file is bizarrly incorrect # sometimes -- files are listed as C++, plain text (when they're # latex), or even 'data'. As long as the output of 'file' has the # word 'text' in it, I treat it as latex. # # The problem with files not being recognized as latex after # gunzipping apparently has to do with text encodings. Here are a few # examples of arxiv id's listed as 'data' after gunzipping. # 1303.5083 1401.5069 1401.5758 1401.5838 1401.6077 1401.6577 # 1401.7056 1402.0695 1402.0700 1402.1495 1402.1968 1402.5880 # # All of the above is for file v5.04 on OS X 10.9.2 Mavaricks. This # version of file dates from about 2010 and I'm writing this in 2014. # Going to the latest version of file v5.17 results in improvements in # the file type determination for about 300 of the ~3000 files in the # entire archive of 1 million papers, so, it's not really worth it. # # In any case, using file v5.17 installed from MacPorts results in # messages like: # ERROR: line 163: regex error 17, (illegal byte sequence) # This is evidently another text encoding problem and it is discussed here: # https://trac.macports.org/ticket/38771 # A workaround is to set the shell variables: # export LC_CTYPE=C # export LANG=C # # There's other weird stuff in the files provided by arxiv.org, like # PDF files that 'file' reports to be strange things. All of these # are viewable via Emacs DocView and Apple Preview, although when you # look at the bytes they do indeed look weird (not like normal PDF # files). # ./0003/cond-mat0003162.pdf: data # ./0304/cond-mat0304406.pdf: MacBinary III data with surprising version number # ./0404/physics0404071.pdf: data # ./9803/cond-mat9803359.pdf: data # ./9805/cond-mat9805146.pdf: data # ./0402/cs0402023.pdf: POSIX tar archive (GNU) # ./1204/1204.0257.pdf: data # ./1204/1204.0258.pdf: data # # It takes ~5.5 min to just gunzip everything and untar everything via # the shell for one month's sumbissions. takes ~10 min to do it via # python code. I find this acceptable. #
# # -*- coding: utf-8 -*- # ############################################################################## # # # # OpenERP, Open Source Management Solution # # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>). # # # # This program is free software: you can redistribute it and/or modify # # it under the terms of the GNU Affero General Public License as # # published by the Free Software Foundation, either version 3 of the # # License, or (at your option) any later version. # # # # This program is distributed in the hope that it will be useful, # # but WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # # GNU Affero General Public License for more details. # # # # You should have received a copy of the GNU Affero General Public License # # along with this program. If not, see <http://www.gnu.org/licenses/>. # # # ############################################################################## # # import time # # from openerp.osv import fields, osv # from openerp.tools.translate import _ # # # class account_invoice_debit(osv.TransientModel): # # """Debits Note from Invoice""" # # _name = "account.invoice.debit" # _description = "Invoice Debit Note" # _columns = { # 'date': fields.date('Operation date', # help='This date will be used as the invoice date ' # 'for Refund Invoice and Period will be ' # 'chosen accordingly!'), # 'period': fields.many2one('account.period', 'Force period'), # 'journal_id': fields.many2one('account.journal', # 'Refund Journal', # help='You can select here the journal ' # 'to use for the refund invoice ' # 'that will be created. If you ' # 'leave that field empty, it will ' # 'use the same journal as the ' # 'current invoice.'), # 'description': fields.char('Description', size=128, required=True), # 'comment': fields.text('Comment', required=True), # } # # def _get_journal(self, cr, uid, context=None): # obj_journal = self.pool.get('account.journal') # user_obj = self.pool.get('res.users') # if context is None: # context = {} # inv_type = context.get('type', 'out_invoice') # company_id = user_obj.browse( # cr, uid, uid, context=context).company_id.id # type = (inv_type == 'out_invoice') and 'sale_refund' or \ # (inv_type == 'out_refund') and 'sale' or \ # (inv_type == 'in_invoice') and 'purchase_refund' or \ # (inv_type == 'in_refund') and 'purchase' # journal = obj_journal.search(cr, uid, [('type', '=', type), ( # 'company_id', '=', company_id)], limit=1, context=context) # return journal and journal[0] or False # # _defaults = { # 'date': lambda *a: time.strftime('%Y-%m-%d'), # 'journal_id': _get_journal, # } # # def fields_view_get(self, cr, uid, view_id=None, view_type=False, # context=None, toolbar=False, submenu=False): # if context is None: # context = {} # journal_obj = self.pool.get('account.journal') # res = super(account_invoice_debit, self).fields_view_get( # cr, uid, view_id=view_id, view_type=view_type, context=context, # toolbar=toolbar, submenu=submenu) # # Debit note only from customer o purchase invoice # # type = context.get('journal_type', 'sale_refund') # type = context.get('journal_type', 'sale') # if type in ('sale', 'sale_refund'): # type = 'sale' # else: # type = 'purchase' # for field in res['fields']: # if field == 'journal_id': # journal_select = journal_obj._name_search(cr, uid, '', [( # 'type', '=', type)], context=context, limit=None, # name_get_uid=1) # res['fields'][field]['selection'] = journal_select # return res # # def _get_period(self, cr, uid, context={}): # """ # Return default account period value # """ # account_period_obj = self.pool.get('account.period') # ids = account_period_obj.find(cr, uid, context=context) # period_id = False # if ids: # period_id = ids[0] # return period_id # # def _get_orig(self, cr, uid, inv, ref, context={}): # """ # Return default origin value # """ # nro_ref = ref # if inv.type == 'out_invoice': # nro_ref = inv.number # orig = _('INV:') + (nro_ref or '') + _('- DATE:') + ( # inv.date_invoice or '') + (' TOTAL:' + str(inv.amount_total) or '') # return orig # # def compute_debit(self, cr, uid, ids, context=None): # """ # @param cr: the current row, from the database cursor, # @param uid: the current user’s ID for security checks, # @param ids: the account invoice refund’s ID or list of IDs # # """ # inv_obj = self.pool.get('account.invoice') # mod_obj = self.pool.get('ir.model.data') # act_obj = self.pool.get('ir.actions.act_window') # inv_tax_obj = self.pool.get('account.invoice.tax') # inv_line_obj = self.pool.get('account.invoice.line') # res_users_obj = self.pool.get('res.users') # if context is None: # context = {} # # for form in self.browse(cr, uid, ids, context=context): # created_inv = [] # date = False # period = False # description = False # company = res_users_obj.browse( # cr, uid, uid, context=context).company_id # journal_id = form.journal_id.id # for inv in inv_obj.browse(cr, uid, context.get('active_ids'), # context=context): # if inv.state in ['draft', 'proforma2', 'cancel']: # raise osv.except_osv(_('Error !'), _( # 'Can not create a debit note from ' # 'draft/proforma/cancel invoice.')) # if inv.reconciled and mode in ('cancel', 'modify'): # raise osv.except_osv(_('Error!'), _( # 'Cannot %s invoice which is already reconciled, ' # 'invoice should be unreconciled first. You can only ' # 'refund this invoice.') % (mode)) # if form.period.id: # period = form.period.id # else: # # Take period from the current date # # period = inv.period_id and inv.period_id.id or False # period = self._get_period(cr, uid, context) # # if not journal_id: # journal_id = inv.journal_id.id # # if form.date: # date = form.date # if not form.period.id: # cr.execute("select name from ir_model_fields \ # where model = 'account.period' \ # and name = 'company_id'") # result_query = cr.fetchone() # if result_query: # # in multi company mode # cr.execute("""select p.id from account_fiscalyear \ # y, account_period p where y.id=p.fiscalyear_id \ # and date(%s) between p.date_start AND \ # p.date_stop and y.company_id = %s limit 1""", # (date, company.id,)) # else: # # in mono company mode # cr.execute("""SELECT id # from account_period where date(%s) # between date_start AND date_stop \ # limit 1 """, (date,)) # res = cr.fetchone() # if res: # period = res[0] # else: # date = inv.date_invoice # if form.description: # description = form.description # else: # description = inv.name # # if not period: # raise osv.except_osv(_('Insufficient Data!'), # _('No period found on the invoice.')) # # # we get original data of invoice to create a new invoice that # # is the copy of the original # invoice = inv_obj.read(cr, uid, [inv.id], # ['name', 'type', 'number', 'reference', # 'comment', 'date_due', 'partner_id', # 'partner_insite', 'partner_contact', # 'partner_ref', 'payment_term', # 'account_id', 'currency_id', # 'invoice_line', 'tax_line', # 'journal_id', 'period_id'], # context=context) # invoice = invoice[0] # del invoice['id'] # invoice_lines = inv_line_obj.browse( # cr, uid, invoice['invoice_line'], context=context) # invoice_lines = inv_obj._refund_cleanup_lines( # cr, uid, invoice_lines, context=context) # tax_lines = inv_tax_obj.browse( # cr, uid, invoice['tax_line'], context=context) # tax_lines = inv_obj._refund_cleanup_lines( # cr, uid, tax_lines, context=context) # # Add origin, parent and comment values # orig = self._get_orig(cr, uid, inv, invoice[ # 'reference'], context) # invoice.update({ # 'type': inv.type == 'in_invoice' and 'in_refund' or # inv.type == 'out_invoice' and 'out_refund', # 'date_invoice': date, # 'state': 'draft', # 'number': False, # 'invoice_line': invoice_lines, # 'tax_line': tax_lines, # 'period_id': period, # 'parent_id': inv.id, # 'name': description, # 'origin': orig, # 'comment': form['comment'] # }) # # take the id part of the tuple returned for many2one fields # for field in ('partner_id', 'account_id', 'currency_id', # 'payment_term', 'journal_id'): # invoice[field] = invoice[field] and invoice[field][0] # # create the new invoice # inv_id = inv_obj.create(cr, uid, invoice, {}) # # we compute due date # if inv.payment_term.id: # data = inv_obj.onchange_payment_term_date_invoice( # cr, uid, [inv_id], inv.payment_term.id, date) # if 'value' in data and data['value']: # inv_obj.write(cr, uid, [inv_id], data['value']) # created_inv.append(inv_id) # # we get the view id # xml_id = (inv.type == 'out_refund') and 'action_invoice_tree1' or \ # (inv.type == 'in_refund') and 'action_invoice_tree2' or \ # (inv.type == 'out_invoice') and 'action_invoice_tree3' or \ # (inv.type == 'in_invoice') and 'action_invoice_tree4' # # we get the model # result = mod_obj.get_object_reference(cr, uid, 'account', xml_id) # id = result and result[1] or False # # we read the act window # result = act_obj.read(cr, uid, id, context=context) # # we add the new invoices into domain list # invoice_domain = eval(result['domain']) # invoice_domain.append(('id', 'in', created_inv)) # result['domain'] = invoice_domain # return result # # def invoice_debit(self, cr, uid, ids, context=None): # return self.compute_debit(cr, uid, ids, context=context) # # # # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
"""Drag-and-drop support for Tkinter. This is very preliminary. I currently only support dnd *within* one application, between different windows (or within the same window). I an trying to make this as generic as possible -- not dependent on the use of a particular widget or icon type, etc. I also hope that this will work with Pmw. To enable an object to be dragged, you must create an event binding for it that starts the drag-and-drop process. Typically, you should bind <ButtonPress> to a callback function that you write. The function should call Tkdnd.dnd_start(source, event), where 'source' is the object to be dragged, and 'event' is the event that invoked the call (the argument to your callback function). Even though this is a class instantiation, the returned instance should not be stored -- it will be kept alive automatically for the duration of the drag-and-drop. When a drag-and-drop is already in process for the Tk interpreter, the call is *ignored*; this normally averts starting multiple simultaneous dnd processes, e.g. because different button callbacks all dnd_start(). The object is *not* necessarily a widget -- it can be any application-specific object that is meaningful to potential drag-and-drop targets. Potential drag-and-drop targets are discovered as follows. Whenever the mouse moves, and at the start and end of a drag-and-drop move, the Tk widget directly under the mouse is inspected. This is the target widget (not to be confused with the target object, yet to be determined). If there is no target widget, there is no dnd target object. If there is a target widget, and it has an attribute dnd_accept, this should be a function (or any callable object). The function is called as dnd_accept(source, event), where 'source' is the object being dragged (the object passed to dnd_start() above), and 'event' is the most recent event object (generally a <Motion> event; it can also be <ButtonPress> or <ButtonRelease>). If the dnd_accept() function returns something other than None, this is the new dnd target object. If dnd_accept() returns None, or if the target widget has no dnd_accept attribute, the target widget's parent is considered as the target widget, and the search for a target object is repeated from there. If necessary, the search is repeated all the way up to the root widget. If none of the target widgets can produce a target object, there is no target object (the target object is None). The target object thus produced, if any, is called the new target object. It is compared with the old target object (or None, if there was no old target widget). There are several cases ('source' is the source object, and 'event' is the most recent event object): - Both the old and new target objects are None. Nothing happens. - The old and new target objects are the same object. Its method dnd_motion(source, event) is called. - The old target object was None, and the new target object is not None. The new target object's method dnd_enter(source, event) is called. - The new target object is None, and the old target object is not None. The old target object's method dnd_leave(source, event) is called. - The old and new target objects differ and neither is None. The old target object's method dnd_leave(source, event), and then the new target object's method dnd_enter(source, event) is called. Once this is done, the new target object replaces the old one, and the Tk mainloop proceeds. The return value of the methods mentioned above is ignored; if they raise an exception, the normal exception handling mechanisms take over. The drag-and-drop processes can end in two ways: a final target object is selected, or no final target object is selected. When a final target object is selected, it will always have been notified of the potential drop by a call to its dnd_enter() method, as described above, and possibly one or more calls to its dnd_motion() method; its dnd_leave() method has not been called since the last call to dnd_enter(). The target is notified of the drop by a call to its method dnd_commit(source, event). If no final target object is selected, and there was an old target object, its dnd_leave(source, event) method is called to complete the dnd sequence. Finally, the source object is notified that the drag-and-drop process is over, by a call to source.dnd_end(target, event), specifying either the selected target object, or None if no target object was selected. The source object can use this to implement the commit action; this is sometimes simpler than to do it in the target's dnd_commit(). The target's dnd_commit() method could then simply be aliased to dnd_leave(). At any time during a dnd sequence, the application can cancel the sequence by calling the cancel() method on the object returned by dnd_start(). This will call dnd_leave() if a target is currently active; it will never call dnd_commit(). """
""" This page is in the table of contents. The obj.py script is an import translator plugin to get a carving from an obj file. An example obj file is box.obj in the models folder. An import plugin is a script in the interpret_plugins folder which has the function getCarving. It is meant to be run from the interpret tool. To ensure that the plugin works on platforms which do not handle file capitalization properly, give the plugin a lower case name. The getCarving function takes the file name of an obj file and returns the carving. From wikipedia, OBJ (or .OBJ) is a geometry definition file format first developed by Wavefront Technologies for its Advanced Visualizer animation package: http://en.wikipedia.org/wiki/Obj The Object File specification is at: http://local.wasp.uwa.edu.au/~pbourke/dataformats/obj/ An excellent link page about obj files is at: http://people.sc.fsu.edu/~burkardt/data/obj/obj.html This example gets a carving for the obj file box.obj. This example is run in a terminal in the folder which contains box.obj and obj.py. > python Python 2.5.1 (r251:54863, Sep 22 2007, 01:43:31) [GCC 4.2.1 (SUSE Linux)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import obj >>> obj.getCarving() [-62.0579, -41.4791, 0.0, 58.8424, -41.4791, 0.0, -62.0579, 22.1865, 0.0, 58.8424, 22.1865, 0.0, -62.0579, -41.4791, 39.8714, 58.8424, -41.4791, 39.8714, -62.0579, 22.1865, 39.8714, 58.8424, 22.1865, 39.8714] [0 [0, 10] [0, 2], 1 [0, 1] [0, 3], 2 [0, 8] [2, 3], 3 [1, 6] [1, 3], 4 [1, 4] [0, 1], 5 [2, 5] [4, 5], 6 [2, 3] [4, 7], 7 [2, 7] [5, 7], 8 [3, 9] [6, 7], 9 [3, 11] [4, 6], 10 [4, 5] [0, 5], 11 [4, 7] [1, 5], 12 [5, 10] [0, 4], 13 [6, 7] [1, 7], 14 [6, 9] [3, 7], 15 [8, 9] [3, 6], 16 [8, 11] [2, 6], 17 [10, 11] [2, 4]] [0 [0, 1, 2] [0, 2, 3], 1 [3, 1, 4] [3, 1, 0], 2 [5, 6, 7] [4, 5, 7], 3 [8, 6, 9] [7, 6, 4], 4 [4, 10, 11] [0, 1, 5], 5 [5, 10, 12] [5, 4, 0], 6 [3, 13, 14] [1, 3, 7], 7 [7, 13, 11] [7, 5, 1], 8 [2, 15, 16] [3, 2, 6], 9 [8, 15, 14] [6, 7, 3], 10 [0, 17, 12] [2, 0, 4], 11 [9, 17, 16] [4, 6, 2]][11.6000003815, 10.6837882996, 7.80209827423 """
# # ElementTree # $Id: ElementTree.py 2326 2005-03-17 07:45:21Z USERNAME $ # # light-weight XML support for Python 1.5.2 and later. # # history: # 2001-10-20 fl created (from various sources) # 2001-11-01 fl return root from parse method # 2002-02-16 fl sort attributes in lexical order # 2002-04-06 fl TreeBuilder refactoring, added PythonDoc markup # 2002-05-01 fl finished TreeBuilder refactoring # 2002-07-14 fl added basic namespace support to ElementTree.write # 2002-07-25 fl added QName attribute support # 2002-10-20 fl fixed encoding in write # 2002-11-24 fl changed default encoding to ascii; fixed attribute encoding # 2002-11-27 fl accept file objects or file names for parse/write # 2002-12-04 fl moved XMLTreeBuilder back to this module # 2003-01-11 fl fixed entity encoding glitch for us-ascii # 2003-02-13 fl added XML literal factory # 2003-02-21 fl added ProcessingInstruction/PI factory # 2003-05-11 fl added tostring/fromstring helpers # 2003-05-26 fl added ElementPath support # 2003-07-05 fl added makeelement factory method # 2003-07-28 fl added more well-known namespace prefixes # 2003-08-15 fl fixed typo in ElementTree.findtext (Thomas NAME 2003-09-04 fl fall back on emulator if ElementPath is not installed # 2003-10-31 fl markup updates # 2003-11-15 fl fixed nested namespace bug # 2004-03-28 fl added XMLID helper # 2004-06-02 fl added default support to findtext # 2004-06-08 fl fixed encoding of non-ascii element/attribute names # 2004-08-23 fl take advantage of post-2.1 expat features # 2005-02-01 fl added iterparse implementation # 2005-03-02 fl fixed iterparse support for pre-2.2 versions # # Copyright (c) 1999-2005 by NAME All rights reserved. # # EMAIL # http://www.pythonware.com # # -------------------------------------------------------------------- # The ElementTree toolkit is # # Copyright (c) 1999-2005 by NAME By obtaining, using, and/or copying this software and/or its # associated documentation, you agree that you have read, understood, # and will comply with the following terms and conditions: # # Permission to use, copy, modify, and distribute this software and # its associated documentation for any purpose and without fee is # hereby granted, provided that the above copyright notice appears in # all copies, and that both that copyright notice and this permission # notice appear in supporting documentation, and that the name of # Secret Labs AB or the author not be used in advertising or publicity # pertaining to distribution of the software without specific, written # prior permission. # # SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD # TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT- # ABILITY AND FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE # OF THIS SOFTWARE. # --------------------------------------------------------------------
""" ===================================================== Optimization and root finding (:mod:`scipy.optimize`) ===================================================== .. currentmodule:: scipy.optimize Optimization ============ Local Optimization ------------------ .. autosummary:: :toctree: generated/ minimize - Unified interface for minimizers of multivariate functions minimize_scalar - Unified interface for minimizers of univariate functions OptimizeResult - The optimization result returned by some optimizers The `minimize` function supports the following methods: .. toctree:: optimize.minimize-neldermead optimize.minimize-powell optimize.minimize-cg optimize.minimize-bfgs optimize.minimize-newtoncg optimize.minimize-lbfgsb optimize.minimize-tnc optimize.minimize-cobyla optimize.minimize-slsqp optimize.minimize-dogleg optimize.minimize-trustncg The `minimize_scalar` function supports the following methods: .. toctree:: optimize.minimize_scalar-brent optimize.minimize_scalar-bounded optimize.minimize_scalar-golden The specific optimization method interfaces below in this subsection are not recommended for use in new scripts; all of these methods are accessible via a newer, more consistent interface provided by the functions above. General-purpose multivariate methods: .. autosummary:: :toctree: generated/ fmin - Nelder-Mead Simplex algorithm fmin_powell - Powell's (modified) level set method fmin_cg - Non-linear (Polak-Ribiere) conjugate gradient algorithm fmin_bfgs - Quasi-Newton method (Broydon-Fletcher-Goldfarb-Shanno) fmin_ncg - Line-search Newton Conjugate Gradient Constrained multivariate methods: .. autosummary:: :toctree: generated/ fmin_l_bfgs_b - Zhu, Byrd, and Nocedal's constrained optimizer fmin_tnc - Truncated Newton code fmin_cobyla - Constrained optimization by linear approximation fmin_slsqp - Minimization using sequential least-squares programming differential_evolution - stochastic minimization using differential evolution Univariate (scalar) minimization methods: .. autosummary:: :toctree: generated/ fminbound - Bounded minimization of a scalar function brent - 1-D function minimization using Brent method golden - 1-D function minimization using Golden Section method Equation (Local) Minimizers --------------------------- .. autosummary:: :toctree: generated/ leastsq - Minimize the sum of squares of M equations in N unknowns nnls - Linear least-squares problem with non-negativity constraint Global Optimization ------------------- .. autosummary:: :toctree: generated/ basinhopping - Basinhopping stochastic optimizer brute - Brute force searching optimizer differential_evolution - stochastic minimization using differential evolution Rosenbrock function ------------------- .. autosummary:: :toctree: generated/ rosen - The Rosenbrock function. rosen_der - The derivative of the Rosenbrock function. rosen_hess - The Hessian matrix of the Rosenbrock function. rosen_hess_prod - Product of the Rosenbrock Hessian with a vector. Fitting ======= .. autosummary:: :toctree: generated/ curve_fit -- Fit curve to a set of points Root finding ============ Scalar functions ---------------- .. autosummary:: :toctree: generated/ brentq - quadratic interpolation Brent method brenth - Brent method, modified by Harris with hyperbolic extrapolation ridder - Ridder's method bisect - Bisection method newton - Secant method or Newton's method Fixed point finding: .. autosummary:: :toctree: generated/ fixed_point - Single-variable fixed-point solver Multidimensional ---------------- General nonlinear solvers: .. autosummary:: :toctree: generated/ root - Unified interface for nonlinear solvers of multivariate functions fsolve - Non-linear multi-variable equation solver broyden1 - Broyden's first method broyden2 - Broyden's second method The `root` function supports the following methods: .. toctree:: optimize.root-hybr optimize.root-lm optimize.root-broyden1 optimize.root-broyden2 optimize.root-anderson optimize.root-linearmixing optimize.root-diagbroyden optimize.root-excitingmixing optimize.root-krylov optimize.root-dfsane Large-scale nonlinear solvers: .. autosummary:: :toctree: generated/ newton_krylov anderson Simple iterations: .. autosummary:: :toctree: generated/ excitingmixing linearmixing diagbroyden :mod:`Additional information on the nonlinear solvers <scipy.optimize.nonlin>` Linear Programming ================== Simplex Algorithm: .. autosummary:: :toctree: generated/ linprog -- Linear programming using the simplex algorithm The `linprog` function supports the following methods: .. toctree:: optimize.linprog-simplex Utilities ========= .. autosummary:: :toctree: generated/ approx_fprime - Approximate the gradient of a scalar function bracket - Bracket a minimum, given two starting points check_grad - Check the supplied derivative using finite differences line_search - Return a step that satisfies the strong Wolfe conditions show_options - Show specific options optimization solvers LbfgsInvHessProduct - Linear operator for L-BFGS approximate inverse Hessian """
"""This module tests SyntaxErrors. Here's an example of the sort of thing that is tested. >>> def f(x): ... global x Traceback (most recent call last): SyntaxError: name 'x' is parameter and global The tests are all raise SyntaxErrors. They were created by checking each C call that raises SyntaxError. There are several modules that raise these exceptions-- ast.c, compile.c, future.c, pythonrun.c, and symtable.c. The parser itself outlaws a lot of invalid syntax. None of these errors are tested here at the moment. We should add some tests; since there are infinitely many programs with invalid syntax, we would need to be judicious in selecting some. The compiler generates a synthetic module name for code executed by doctest. Since all the code comes from the same module, a suffix like [1] is appended to the module name, As a consequence, changing the order of tests in this module means renumbering all the errors after it. (Maybe we should enable the ellipsis option for these tests.) In ast.c, syntax errors are raised by calling ast_error(). Errors from set_context(): >>> obj.None = 1 Traceback (most recent call last): SyntaxError: invalid syntax >>> None = 1 Traceback (most recent call last): SyntaxError: assignment to keyword It's a syntax error to assign to the empty tuple. Why isn't it an error to assign to the empty list? It will always raise some error at runtime. >>> () = 1 Traceback (most recent call last): SyntaxError: can't assign to () >>> f() = 1 Traceback (most recent call last): SyntaxError: can't assign to function call >>> del f() Traceback (most recent call last): SyntaxError: can't delete function call >>> a + 1 = 2 Traceback (most recent call last): SyntaxError: can't assign to operator >>> (x for x in x) = 1 Traceback (most recent call last): SyntaxError: can't assign to generator expression >>> 1 = 1 Traceback (most recent call last): SyntaxError: can't assign to literal >>> "abc" = 1 Traceback (most recent call last): SyntaxError: can't assign to literal >>> b"" = 1 Traceback (most recent call last): SyntaxError: can't assign to literal >>> `1` = 1 Traceback (most recent call last): SyntaxError: invalid syntax If the left-hand side of an assignment is a list or tuple, an illegal expression inside that contain should still cause a syntax error. This test just checks a couple of cases rather than enumerating all of them. >>> (a, "b", c) = (1, 2, 3) Traceback (most recent call last): SyntaxError: can't assign to literal >>> [a, b, c + 1] = [1, 2, 3] Traceback (most recent call last): SyntaxError: can't assign to operator >>> a if 1 else b = 1 Traceback (most recent call last): SyntaxError: can't assign to conditional expression From compiler_complex_args(): >>> def f(None=1): ... pass Traceback (most recent call last): SyntaxError: invalid syntax From ast_for_arguments(): >>> def f(x, y=1, z): ... pass Traceback (most recent call last): SyntaxError: non-default argument follows default argument >>> def f(x, None): ... pass Traceback (most recent call last): SyntaxError: invalid syntax >>> def f(*None): ... pass Traceback (most recent call last): SyntaxError: invalid syntax >>> def f(**None): ... pass Traceback (most recent call last): SyntaxError: invalid syntax From ast_for_funcdef(): >>> def None(x): ... pass Traceback (most recent call last): SyntaxError: invalid syntax From ast_for_call(): >>> def f(it, *varargs): ... return list(it) >>> L = range(10) >>> f(x for x in L) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> f(x for x in L, 1) Traceback (most recent call last): SyntaxError: Generator expression must be parenthesized if not sole argument >>> f((x for x in L), 1) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> f(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, ... i12, i13, i14, i15, i16, i17, i18, i19, i20, i21, i22, ... i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33, ... i34, i35, i36, i37, i38, i39, i40, i41, i42, i43, i44, ... i45, i46, i47, i48, i49, i50, i51, i52, i53, i54, i55, ... i56, i57, i58, i59, i60, i61, i62, i63, i64, i65, i66, ... i67, i68, i69, i70, i71, i72, i73, i74, i75, i76, i77, ... i78, i79, i80, i81, i82, i83, i84, i85, i86, i87, i88, ... i89, i90, i91, i92, i93, i94, i95, i96, i97, i98, i99, ... i100, i101, i102, i103, i104, i105, i106, i107, i108, ... i109, i110, i111, i112, i113, i114, i115, i116, i117, ... i118, i119, i120, i121, i122, i123, i124, i125, i126, ... i127, i128, i129, i130, i131, i132, i133, i134, i135, ... i136, i137, i138, i139, i140, i141, i142, i143, i144, ... i145, i146, i147, i148, i149, i150, i151, i152, i153, ... i154, i155, i156, i157, i158, i159, i160, i161, i162, ... i163, i164, i165, i166, i167, i168, i169, i170, i171, ... i172, i173, i174, i175, i176, i177, i178, i179, i180, ... i181, i182, i183, i184, i185, i186, i187, i188, i189, ... i190, i191, i192, i193, i194, i195, i196, i197, i198, ... i199, i200, i201, i202, i203, i204, i205, i206, i207, ... i208, i209, i210, i211, i212, i213, i214, i215, i216, ... i217, i218, i219, i220, i221, i222, i223, i224, i225, ... i226, i227, i228, i229, i230, i231, i232, i233, i234, ... i235, i236, i237, i238, i239, i240, i241, i242, i243, ... i244, i245, i246, i247, i248, i249, i250, i251, i252, ... i253, i254, i255) Traceback (most recent call last): SyntaxError: more than 255 arguments The actual error cases counts positional arguments, keyword arguments, and generator expression arguments separately. This test combines the three. >>> f(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, ... i12, i13, i14, i15, i16, i17, i18, i19, i20, i21, i22, ... i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33, ... i34, i35, i36, i37, i38, i39, i40, i41, i42, i43, i44, ... i45, i46, i47, i48, i49, i50, i51, i52, i53, i54, i55, ... i56, i57, i58, i59, i60, i61, i62, i63, i64, i65, i66, ... i67, i68, i69, i70, i71, i72, i73, i74, i75, i76, i77, ... i78, i79, i80, i81, i82, i83, i84, i85, i86, i87, i88, ... i89, i90, i91, i92, i93, i94, i95, i96, i97, i98, i99, ... i100, i101, i102, i103, i104, i105, i106, i107, i108, ... i109, i110, i111, i112, i113, i114, i115, i116, i117, ... i118, i119, i120, i121, i122, i123, i124, i125, i126, ... i127, i128, i129, i130, i131, i132, i133, i134, i135, ... i136, i137, i138, i139, i140, i141, i142, i143, i144, ... i145, i146, i147, i148, i149, i150, i151, i152, i153, ... i154, i155, i156, i157, i158, i159, i160, i161, i162, ... i163, i164, i165, i166, i167, i168, i169, i170, i171, ... i172, i173, i174, i175, i176, i177, i178, i179, i180, ... i181, i182, i183, i184, i185, i186, i187, i188, i189, ... i190, i191, i192, i193, i194, i195, i196, i197, i198, ... i199, i200, i201, i202, i203, i204, i205, i206, i207, ... i208, i209, i210, i211, i212, i213, i214, i215, i216, ... i217, i218, i219, i220, i221, i222, i223, i224, i225, ... i226, i227, i228, i229, i230, i231, i232, i233, i234, ... i235, i236, i237, i238, i239, i240, i241, i242, i243, ... (x for x in i244), i245, i246, i247, i248, i249, i250, i251, ... i252=1, i253=1, i254=1, i255=1) Traceback (most recent call last): SyntaxError: more than 255 arguments >>> f(lambda x: x[0] = 3) Traceback (most recent call last): SyntaxError: lambda cannot contain assignment The grammar accepts any test (basically, any expression) in the keyword slot of a call site. Test a few different options. >>> f(x()=2) Traceback (most recent call last): SyntaxError: keyword can't be an expression >>> f(a or b=1) Traceback (most recent call last): SyntaxError: keyword can't be an expression >>> f(x.y=1) Traceback (most recent call last): SyntaxError: keyword can't be an expression More set_context(): >>> (x for x in x) += 1 Traceback (most recent call last): SyntaxError: can't assign to generator expression >>> None += 1 Traceback (most recent call last): SyntaxError: assignment to keyword >>> f() += 1 Traceback (most recent call last): SyntaxError: can't assign to function call Test continue in finally in weird combinations. continue in for loop under finally should be ok. >>> def test(): ... try: ... pass ... finally: ... for abc in range(10): ... continue ... print(abc) >>> test() 9 Start simple, a continue in a finally should not be allowed. >>> def test(): ... for abc in range(10): ... try: ... pass ... finally: ... continue Traceback (most recent call last): ... SyntaxError: 'continue' not supported inside 'finally' clause This is essentially a continue in a finally which should not be allowed. >>> def test(): ... for abc in range(10): ... try: ... pass ... finally: ... try: ... continue ... except: ... pass Traceback (most recent call last): ... SyntaxError: 'continue' not supported inside 'finally' clause >>> def foo(): ... try: ... pass ... finally: ... continue Traceback (most recent call last): ... SyntaxError: 'continue' not supported inside 'finally' clause >>> def foo(): ... for a in (): ... try: ... pass ... finally: ... continue Traceback (most recent call last): ... SyntaxError: 'continue' not supported inside 'finally' clause >>> def foo(): ... for a in (): ... try: ... pass ... finally: ... try: ... continue ... finally: ... pass Traceback (most recent call last): ... SyntaxError: 'continue' not supported inside 'finally' clause >>> def foo(): ... for a in (): ... try: pass ... finally: ... try: ... pass ... except: ... continue Traceback (most recent call last): ... SyntaxError: 'continue' not supported inside 'finally' clause There is one test for a break that is not in a loop. The compiler uses a single data structure to keep track of try-finally and loops, so we need to be sure that a break is actually inside a loop. If it isn't, there should be a syntax error. >>> try: ... print(1) ... break ... print(2) ... finally: ... print(3) Traceback (most recent call last): ... SyntaxError: 'break' outside loop This should probably raise a better error than a SystemError (or none at all). In 2.5 there was a missing exception and an assert was triggered in a debug build. The number of blocks must be greater than CO_MAXBLOCKS. SF #1565514 >>> while 1: ... while 2: ... while 3: ... while 4: ... while 5: ... while 6: ... while 8: ... while 9: ... while 10: ... while 11: ... while 12: ... while 13: ... while 14: ... while 15: ... while 16: ... while 17: ... while 18: ... while 19: ... while 20: ... while 21: ... while 22: ... break Traceback (most recent call last): ... SystemError: too many statically nested blocks Misuse of the nonlocal statement can lead to a few unique syntax errors. >>> def f(x): ... nonlocal x Traceback (most recent call last): ... SyntaxError: name 'x' is parameter and nonlocal >>> def f(): ... global x ... nonlocal x Traceback (most recent call last): ... SyntaxError: name 'x' is nonlocal and global >>> def f(): ... nonlocal x Traceback (most recent call last): ... SyntaxError: no binding for nonlocal 'x' found From SF bug #1705365 >>> nonlocal x Traceback (most recent call last): ... SyntaxError: nonlocal declaration not allowed at module level TODO(jhylton): Figure out how to test SyntaxWarning with doctest. ## >>> def f(x): ## ... def f(): ## ... print(x) ## ... nonlocal x ## Traceback (most recent call last): ## ... ## SyntaxWarning: name 'x' is assigned to before nonlocal declaration ## >>> def f(): ## ... x = 1 ## ... nonlocal x ## Traceback (most recent call last): ## ... ## SyntaxWarning: name 'x' is assigned to before nonlocal declaration This tests assignment-context; there was a bug in Python 2.5 where compiling a complex 'if' (one with 'elif') would fail to notice an invalid suite, leading to spurious errors. >>> if 1: ... x() = 1 ... elif 1: ... pass Traceback (most recent call last): ... SyntaxError: can't assign to function call >>> if 1: ... pass ... elif 1: ... x() = 1 Traceback (most recent call last): ... SyntaxError: can't assign to function call >>> if 1: ... x() = 1 ... elif 1: ... pass ... else: ... pass Traceback (most recent call last): ... SyntaxError: can't assign to function call >>> if 1: ... pass ... elif 1: ... x() = 1 ... else: ... pass Traceback (most recent call last): ... SyntaxError: can't assign to function call >>> if 1: ... pass ... elif 1: ... pass ... else: ... x() = 1 Traceback (most recent call last): ... SyntaxError: can't assign to function call Make sure that the old "raise X, Y[, Z]" form is gone: >>> raise X, Y Traceback (most recent call last): ... SyntaxError: invalid syntax >>> raise X, Y, Z Traceback (most recent call last): ... SyntaxError: invalid syntax >>> f(a=23, a=234) Traceback (most recent call last): ... SyntaxError: keyword argument repeated >>> del () Traceback (most recent call last): SyntaxError: can't delete () >>> {1, 2, 3} = 42 Traceback (most recent call last): SyntaxError: can't assign to literal Corner-cases that used to fail to raise the correct error: >>> def f(*, x=lambda __debug__:0): pass Traceback (most recent call last): SyntaxError: assignment to keyword >>> def f(*args:(lambda __debug__:0)): pass Traceback (most recent call last): SyntaxError: assignment to keyword >>> def f(**kwargs:(lambda __debug__:0)): pass Traceback (most recent call last): SyntaxError: assignment to keyword >>> with (lambda *:0): pass Traceback (most recent call last): SyntaxError: named arguments must follow bare * Corner-cases that used to crash: >>> def f(**__debug__): pass Traceback (most recent call last): SyntaxError: assignment to keyword >>> def f(*xx, __debug__): pass Traceback (most recent call last): SyntaxError: assignment to keyword """
""" Discrete Fourier Transform (:mod:`numpy.fft`) ============================================= .. currentmodule:: numpy.fft Standard FFTs ------------- .. autosummary:: :toctree: generated/ fft Discrete Fourier transform. ifft Inverse discrete Fourier transform. fft2 Discrete Fourier transform in two dimensions. ifft2 Inverse discrete Fourier transform in two dimensions. fftn Discrete Fourier transform in N-dimensions. ifftn Inverse discrete Fourier transform in N dimensions. Real FFTs --------- .. autosummary:: :toctree: generated/ rfft Real discrete Fourier transform. irfft Inverse real discrete Fourier transform. rfft2 Real discrete Fourier transform in two dimensions. irfft2 Inverse real discrete Fourier transform in two dimensions. rfftn Real discrete Fourier transform in N dimensions. irfftn Inverse real discrete Fourier transform in N dimensions. Hermitian FFTs -------------- .. autosummary:: :toctree: generated/ hfft Hermitian discrete Fourier transform. ihfft Inverse Hermitian discrete Fourier transform. Helper routines --------------- .. autosummary:: :toctree: generated/ fftfreq Discrete Fourier Transform sample frequencies. rfftfreq DFT sample frequencies (for usage with rfft, irfft). fftshift Shift zero-frequency component to center of spectrum. ifftshift Inverse of fftshift. Background information ---------------------- Fourier analysis is fundamentally a method for expressing a function as a sum of periodic components, and for recovering the function from those components. When both the function and its Fourier transform are replaced with discretized counterparts, it is called the discrete Fourier transform (DFT). The DFT has become a mainstay of numerical computing in part because of a very fast algorithm for computing it, called the Fast Fourier Transform (FFT), which was known to Gauss (1805) and was brought to light in its current form by NAME and NAME [CT]_. Press et al. [NR]_ provide an accessible introduction to Fourier analysis and its applications. Because the discrete Fourier transform separates its input into components that contribute at discrete frequencies, it has a great number of applications in digital signal processing, e.g., for filtering, and in this context the discretized input to the transform is customarily referred to as a *signal*, which exists in the *time domain*. The output is called a *spectrum* or *transform* and exists in the *frequency domain*. Implementation details ---------------------- There are many ways to define the DFT, varying in the sign of the exponent, normalization, etc. In this implementation, the DFT is defined as .. math:: A_k = \\sum_{m=0}^{n-1} a_m \\exp\\left\\{-2\\pi i{mk \\over n}\\right\\} \\qquad k = 0,\\ldots,n-1. The DFT is in general defined for complex inputs and outputs, and a single-frequency component at linear frequency :math:`f` is represented by a complex exponential :math:`a_m = \\exp\\{2\\pi i\\,f m\\Delta t\\}`, where :math:`\\Delta t` is the sampling interval. The values in the result follow so-called "standard" order: If ``A = fft(a, n)``, then ``A[0]`` contains the zero-frequency term (the sum of the signal), which is always purely real for real inputs. Then ``A[1:n/2]`` contains the positive-frequency terms, and ``A[n/2+1:]`` contains the negative-frequency terms, in order of decreasingly negative frequency. For an even number of input points, ``A[n/2]`` represents both positive and negative Nyquist frequency, and is also purely real for real input. For an odd number of input points, ``A[(n-1)/2]`` contains the largest positive frequency, while ``A[(n+1)/2]`` contains the largest negative frequency. The routine ``np.fft.fftfreq(n)`` returns an array giving the frequencies of corresponding elements in the output. The routine ``np.fft.fftshift(A)`` shifts transforms and their frequencies to put the zero-frequency components in the middle, and ``np.fft.ifftshift(A)`` undoes that shift. When the input `a` is a time-domain signal and ``A = fft(a)``, ``np.abs(A)`` is its amplitude spectrum and ``np.abs(A)**2`` is its power spectrum. The phase spectrum is obtained by ``np.angle(A)``. The inverse DFT is defined as .. math:: a_m = \\frac{1}{n}\\sum_{k=0}^{n-1}A_k\\exp\\left\\{2\\pi i{mk\\over n}\\right\\} \\qquad m = 0,\\ldots,n-1. It differs from the forward transform by the sign of the exponential argument and the default normalization by :math:`1/n`. Normalization ------------- The default normalization has the direct transforms unscaled and the inverse transforms are scaled by :math:`1/n`. It is possible to obtain unitary transforms by setting the keyword argument ``norm`` to ``"ortho"`` (default is `None`) so that both direct and inverse transforms will be scaled by :math:`1/\\sqrt{n}`. Real and Hermitian transforms ----------------------------- When the input is purely real, its transform is Hermitian, i.e., the component at frequency :math:`f_k` is the complex conjugate of the component at frequency :math:`-f_k`, which means that for real inputs there is no information in the negative frequency components that is not already available from the positive frequency components. The family of `rfft` functions is designed to operate on real inputs, and exploits this symmetry by computing only the positive frequency components, up to and including the Nyquist frequency. Thus, ``n`` input points produce ``n/2+1`` complex output points. The inverses of this family assumes the same symmetry of its input, and for an output of ``n`` points uses ``n/2+1`` input points. Correspondingly, when the spectrum is purely real, the signal is Hermitian. The `hfft` family of functions exploits this symmetry by using ``n/2+1`` complex points in the input (time) domain for ``n`` real points in the frequency domain. In higher dimensions, FFTs are used, e.g., for image analysis and filtering. The computational efficiency of the FFT means that it can also be a faster way to compute large convolutions, using the property that a convolution in the time domain is equivalent to a point-by-point multiplication in the frequency domain. Higher dimensions ----------------- In two dimensions, the DFT is defined as .. math:: A_{kl} = \\sum_{m=0}^{M-1} \\sum_{n=0}^{N-1} a_{mn}\\exp\\left\\{-2\\pi i \\left({mk\\over M}+{nl\\over N}\\right)\\right\\} \\qquad k = 0, \\ldots, M-1;\\quad l = 0, \\ldots, N-1, which extends in the obvious way to higher dimensions, and the inverses in higher dimensions also extend in the same way. References ---------- .. [CT] NAME, NAME and John W. NAME, 1965, "An algorithm for the machine calculation of complex Fourier series," *Math. Comput.* 19: 297-301. .. [NR] NAME NAME NAME and NAME 2007, *Numerical Recipes: The Art of Scientific Computing*, ch. 12-13. Cambridge Univ. Press, Cambridge, UK. Examples -------- For examples, see the various functions. """
""" ========================================== Statistical functions (:mod:`scipy.stats`) ========================================== .. module:: scipy.stats This module contains a large number of probability distributions as well as a growing library of statistical functions. Each univariate distribution is an instance of a subclass of `rv_continuous` (`rv_discrete` for discrete distributions): .. autosummary:: :toctree: generated/ rv_continuous rv_discrete rv_histogram Continuous distributions ======================== .. autosummary:: :toctree: generated/ alpha -- Alpha anglit -- Anglit arcsine -- Arcsine argus -- Argus beta -- Beta betaprime -- Beta Prime bradford -- Bradford burr -- Burr (Type III) burr12 -- Burr (Type XII) cauchy -- Cauchy chi -- Chi chi2 -- Chi-squared cosine -- Cosine crystalball -- Crystalball dgamma -- Double Gamma dweibull -- Double Weibull erlang -- Erlang expon -- Exponential exponnorm -- Exponentially Modified Normal exponweib -- Exponentiated Weibull exponpow -- Exponential Power f -- F (Snecdor F) fatiguelife -- Fatigue Life (Birnbaum-Saunders) fisk -- Fisk foldcauchy -- Folded Cauchy foldnorm -- Folded Normal frechet_r -- Deprecated. Alias for weibull_min frechet_l -- Deprecated. Alias for weibull_max genlogistic -- Generalized Logistic gennorm -- Generalized normal genpareto -- Generalized Pareto genexpon -- Generalized Exponential genextreme -- Generalized Extreme Value gausshyper -- Gauss Hypergeometric gamma -- Gamma gengamma -- Generalized gamma genhalflogistic -- Generalized Half Logistic gilbrat -- Gilbrat gompertz -- Gompertz (Truncated Gumbel) gumbel_r -- Right Sided Gumbel, Log-Weibull, Fisher-Tippett, Extreme Value Type I gumbel_l -- Left Sided Gumbel, etc. halfcauchy -- Half Cauchy halflogistic -- Half Logistic halfnorm -- Half Normal halfgennorm -- Generalized Half Normal hypsecant -- Hyperbolic Secant invgamma -- Inverse Gamma invgauss -- Inverse Gaussian invweibull -- Inverse Weibull johnsonsb -- NAME johnsonsu -- NAME kappa4 -- Kappa 4 parameter kappa3 -- Kappa 3 parameter ksone -- Kolmogorov-Smirnov one-sided (no stats) kstwobign -- Kolmogorov-Smirnov two-sided test for Large N (no stats) laplace -- Laplace levy -- Levy levy_l levy_stable logistic -- Logistic loggamma -- Log-Gamma loglaplace -- Log-Laplace (Log Double Exponential) lognorm -- Log-Normal lomax -- Lomax (Pareto of the second kind) maxwell -- Maxwell mielke -- Mielke's Beta-Kappa nakagami -- Nakagami ncx2 -- Non-central chi-squared ncf -- Non-central F nct -- Non-central Student's T norm -- Normal (Gaussian) pareto -- Pareto pearson3 -- NAME type III powerlaw -- Power-function powerlognorm -- Power log normal powernorm -- Power normal rdist -- R-distribution reciprocal -- Reciprocal rayleigh -- Rayleigh rice -- Rice recipinvgauss -- Reciprocal Inverse Gaussian semicircular -- Semicircular skewnorm -- Skew normal t -- Student's T trapz -- Trapezoidal triang -- Triangular truncexpon -- Truncated Exponential truncnorm -- Truncated Normal tukeylambda -- Tukey-Lambda uniform -- Uniform vonmises -- Von-Mises (Circular) vonmises_line -- Von-Mises (Line) wald -- Wald weibull_min -- Minimum Weibull (see Frechet) weibull_max -- Maximum Weibull (see Frechet) wrapcauchy -- Wrapped Cauchy Multivariate distributions ========================== .. autosummary:: :toctree: generated/ multivariate_normal -- Multivariate normal distribution matrix_normal -- Matrix normal distribution dirichlet -- Dirichlet wishart -- Wishart invwishart -- Inverse Wishart multinomial -- Multinomial distribution special_ortho_group -- SO(N) group ortho_group -- O(N) group unitary_group -- U(N) gropu random_correlation -- random correlation matrices Discrete distributions ====================== .. autosummary:: :toctree: generated/ bernoulli -- Bernoulli binom -- Binomial boltzmann -- Boltzmann (Truncated Discrete Exponential) dlaplace -- Discrete Laplacian geom -- Geometric hypergeom -- Hypergeometric logser -- Logarithmic (Log-Series, Series) nbinom -- Negative Binomial planck -- Planck (Discrete Exponential) poisson -- Poisson randint -- Discrete Uniform skellam -- Skellam zipf -- Zipf Statistical functions ===================== Several of these functions have a similar version in scipy.stats.mstats which work for masked arrays. .. autosummary:: :toctree: generated/ describe -- Descriptive statistics gmean -- Geometric mean hmean -- Harmonic mean kurtosis -- Fisher or NAME kurtosis kurtosistest -- mode -- Modal value moment -- Central moment normaltest -- skew -- Skewness skewtest -- kstat -- kstatvar -- tmean -- Truncated arithmetic mean tvar -- Truncated variance tmin -- tmax -- tstd -- tsem -- variation -- Coefficient of variation find_repeats trim_mean .. autosummary:: :toctree: generated/ cumfreq itemfreq percentileofscore scoreatpercentile relfreq .. autosummary:: :toctree: generated/ binned_statistic -- Compute a binned statistic for a set of data. binned_statistic_2d -- Compute a 2-D binned statistic for a set of data. binned_statistic_dd -- Compute a d-D binned statistic for a set of data. .. autosummary:: :toctree: generated/ obrientransform bayes_mvs mvsdist sem zmap zscore iqr .. autosummary:: :toctree: generated/ sigmaclip trimboth trim1 .. autosummary:: :toctree: generated/ f_oneway pearsonr spearmanr pointbiserialr kendalltau weightedtau linregress theilslopes .. autosummary:: :toctree: generated/ ttest_1samp ttest_ind ttest_ind_from_stats ttest_rel kstest chisquare power_divergence ks_2samp mannwhitneyu tiecorrect rankdata ranksums wilcoxon kruskal friedmanchisquare combine_pvalues jarque_bera .. autosummary:: :toctree: generated/ ansari bartlett levene shapiro anderson anderson_ksamp binom_test fligner median_test mood .. autosummary:: :toctree: generated/ boxcox boxcox_normmax boxcox_llf entropy .. autosummary:: :toctree: generated/ wasserstein_distance energy_distance Circular statistical functions ============================== .. autosummary:: :toctree: generated/ circmean circvar circstd Contingency table functions =========================== .. autosummary:: :toctree: generated/ chi2_contingency contingency.expected_freq contingency.margins fisher_exact Plot-tests ========== .. autosummary:: :toctree: generated/ ppcc_max ppcc_plot probplot boxcox_normplot Masked statistics functions =========================== .. toctree:: stats.mstats Univariate and multivariate kernel density estimation (:mod:`scipy.stats.kde`) ============================================================================== .. autosummary:: :toctree: generated/ gaussian_kde For many more stat related functions install the software R and the interface package rpy. """
# # ElementTree # $Id: ElementTree.py 2326 2005-03-17 07:45:21Z USERNAME $ # # light-weight XML support for Python 1.5.2 and later. # # history: # 2001-10-20 fl created (from various sources) # 2001-11-01 fl return root from parse method # 2002-02-16 fl sort attributes in lexical order # 2002-04-06 fl TreeBuilder refactoring, added PythonDoc markup # 2002-05-01 fl finished TreeBuilder refactoring # 2002-07-14 fl added basic namespace support to ElementTree.write # 2002-07-25 fl added QName attribute support # 2002-10-20 fl fixed encoding in write # 2002-11-24 fl changed default encoding to ascii; fixed attribute encoding # 2002-11-27 fl accept file objects or file names for parse/write # 2002-12-04 fl moved XMLTreeBuilder back to this module # 2003-01-11 fl fixed entity encoding glitch for us-ascii # 2003-02-13 fl added XML literal factory # 2003-02-21 fl added ProcessingInstruction/PI factory # 2003-05-11 fl added tostring/fromstring helpers # 2003-05-26 fl added ElementPath support # 2003-07-05 fl added makeelement factory method # 2003-07-28 fl added more well-known namespace prefixes # 2003-08-15 fl fixed typo in ElementTree.findtext (Thomas NAME 2003-09-04 fl fall back on emulator if ElementPath is not installed # 2003-10-31 fl markup updates # 2003-11-15 fl fixed nested namespace bug # 2004-03-28 fl added XMLID helper # 2004-06-02 fl added default support to findtext # 2004-06-08 fl fixed encoding of non-ascii element/attribute names # 2004-08-23 fl take advantage of post-2.1 expat features # 2005-02-01 fl added iterparse implementation # 2005-03-02 fl fixed iterparse support for pre-2.2 versions # # Copyright (c) 1999-2005 by NAME All rights reserved. # # EMAIL # http://www.pythonware.com # # -------------------------------------------------------------------- # The ElementTree toolkit is # # Copyright (c) 1999-2005 by NAME By obtaining, using, and/or copying this software and/or its # associated documentation, you agree that you have read, understood, # and will comply with the following terms and conditions: # # Permission to use, copy, modify, and distribute this software and # its associated documentation for any purpose and without fee is # hereby granted, provided that the above copyright notice appears in # all copies, and that both that copyright notice and this permission # notice appear in supporting documentation, and that the name of # Secret Labs AB or the author not be used in advertising or publicity # pertaining to distribution of the software without specific, written # prior permission. # # SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD # TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT- # ABILITY AND FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE # OF THIS SOFTWARE. # --------------------------------------------------------------------
""" ======================================== Special functions (:mod:`scipy.special`) ======================================== .. module:: scipy.special Nearly all of the functions below are universal functions and follow broadcasting and automatic array-looping rules. Exceptions are noted. Error handling ============== Errors are handled by returning nans, or other appropriate values. Some of the special function routines will emit warnings when an error occurs. By default this is disabled. To enable such messages use ``errprint(1)``, and to disable such messages use ``errprint(0)``. Example: >>> print scipy.special.bdtr(-1,10,0.3) >>> scipy.special.errprint(1) >>> print scipy.special.bdtr(-1,10,0.3) .. autosummary:: :toctree: generated/ errprint SpecialFunctionWarning -- Warning that can be issued with ``errprint(True)`` Available functions =================== Airy functions -------------- .. autosummary:: :toctree: generated/ airy -- Airy functions and their derivatives. airye -- Exponentially scaled Airy functions ai_zeros -- [+]Zeros of Airy functions Ai(x) and Ai'(x) bi_zeros -- [+]Zeros of Airy functions Bi(x) and Bi'(x) itairy -- Elliptic Functions and Integrals -------------------------------- .. autosummary:: :toctree: generated/ ellipj -- Jacobian elliptic functions ellipk -- Complete elliptic integral of the first kind. ellipkm1 -- ellipkm1(x) == ellipk(1 - x) ellipkinc -- Incomplete elliptic integral of the first kind. ellipe -- Complete elliptic integral of the second kind. ellipeinc -- Incomplete elliptic integral of the second kind. Bessel Functions ---------------- .. autosummary:: :toctree: generated/ jv -- Bessel function of real-valued order and complex argument. jn -- Alias for jv jve -- Exponentially scaled Bessel function. yn -- Bessel function of second kind (integer order). yv -- Bessel function of the second kind (real-valued order). yve -- Exponentially scaled Bessel function of the second kind. kn -- Modified Bessel function of the second kind (integer order). kv -- Modified Bessel function of the second kind (real order). kve -- Exponentially scaled modified Bessel function of the second kind. iv -- Modified Bessel function. ive -- Exponentially scaled modified Bessel function. hankel1 -- Hankel function of the first kind. hankel1e -- Exponentially scaled Hankel function of the first kind. hankel2 -- Hankel function of the second kind. hankel2e -- Exponentially scaled Hankel function of the second kind. The following is not an universal function: .. autosummary:: :toctree: generated/ lmbda -- [+]Sequence of lambda functions with arbitrary order v. Zeros of Bessel Functions ^^^^^^^^^^^^^^^^^^^^^^^^^ These are not universal functions: .. autosummary:: :toctree: generated/ jnjnp_zeros -- [+]Zeros of integer-order Bessel functions and derivatives sorted in order. jnyn_zeros -- [+]Zeros of integer-order Bessel functions and derivatives as separate arrays. jn_zeros -- [+]Zeros of Jn(x) jnp_zeros -- [+]Zeros of Jn'(x) yn_zeros -- [+]Zeros of Yn(x) ynp_zeros -- [+]Zeros of Yn'(x) y0_zeros -- [+]Complex zeros: Y0(z0)=0 and values of Y0'(z0) y1_zeros -- [+]Complex zeros: Y1(z1)=0 and values of Y1'(z1) y1p_zeros -- [+]Complex zeros of Y1'(z1')=0 and values of Y1(z1') Faster versions of common Bessel Functions ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. autosummary:: :toctree: generated/ j0 -- Bessel function of order 0. j1 -- Bessel function of order 1. y0 -- Bessel function of second kind of order 0. y1 -- Bessel function of second kind of order 1. i0 -- Modified Bessel function of order 0. i0e -- Exponentially scaled modified Bessel function of order 0. i1 -- Modified Bessel function of order 1. i1e -- Exponentially scaled modified Bessel function of order 1. k0 -- Modified Bessel function of the second kind of order 0. k0e -- Exponentially scaled modified Bessel function of the second kind of order 0. k1 -- Modified Bessel function of the second kind of order 1. k1e -- Exponentially scaled modified Bessel function of the second kind of order 1. Integrals of Bessel Functions ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. autosummary:: :toctree: generated/ itj0y0 -- Basic integrals of j0 and y0 from 0 to x. it2j0y0 -- Integrals of (1-j0(t))/t from 0 to x and y0(t)/t from x to inf. iti0k0 -- Basic integrals of i0 and k0 from 0 to x. it2i0k0 -- Integrals of (i0(t)-1)/t from 0 to x and k0(t)/t from x to inf. besselpoly -- Integral of a Bessel function: Jv(2* a* x) * x[+]lambda from x=0 to 1. Derivatives of Bessel Functions ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. autosummary:: :toctree: generated/ jvp -- Nth derivative of Jv(v,z) yvp -- Nth derivative of Yv(v,z) kvp -- Nth derivative of Kv(v,z) ivp -- Nth derivative of Iv(v,z) h1vp -- Nth derivative of H1v(v,z) h2vp -- Nth derivative of H2v(v,z) Spherical Bessel Functions ^^^^^^^^^^^^^^^^^^^^^^^^^^ These are not universal functions: .. autosummary:: :toctree: generated/ sph_jn -- [+]Sequence of spherical Bessel functions, jn(z) sph_yn -- [+]Sequence of spherical Bessel functions, yn(z) sph_jnyn -- [+]Sequence of spherical Bessel functions, jn(z) and yn(z) sph_in -- [+]Sequence of spherical Bessel functions, in(z) sph_kn -- [+]Sequence of spherical Bessel functions, kn(z) sph_inkn -- [+]Sequence of spherical Bessel functions, in(z) and kn(z) Riccati-Bessel Functions ^^^^^^^^^^^^^^^^^^^^^^^^ These are not universal functions: .. autosummary:: :toctree: generated/ riccati_jn -- [+]Sequence of Ricatti-Bessel functions of first kind. riccati_yn -- [+]Sequence of Ricatti-Bessel functions of second kind. Struve Functions ---------------- .. autosummary:: :toctree: generated/ struve -- Struve function --- Hv(x) modstruve -- Modified Struve function --- Lv(x) itstruve0 -- Integral of H0(t) from 0 to x it2struve0 -- Integral of H0(t)/t from x to Inf. itmodstruve0 -- Integral of L0(t) from 0 to x. Raw Statistical Functions ------------------------- .. seealso:: :mod:`scipy.stats`: Friendly versions of these functions. .. autosummary:: :toctree: generated/ bdtr -- Sum of terms 0 through k of the binomial pdf. bdtrc -- Sum of terms k+1 through n of the binomial pdf. bdtri -- Inverse of bdtr bdtrik -- bdtrin -- btdtr -- Integral from 0 to x of beta pdf. btdtri -- Quantiles of beta distribution btdtria -- btdtrib -- fdtr -- Integral from 0 to x of F pdf. fdtrc -- Integral from x to infinity under F pdf. fdtri -- Inverse of fdtrc fdtridfd -- gdtr -- Integral from 0 to x of gamma pdf. gdtrc -- Integral from x to infinity under gamma pdf. gdtria -- Inverse with respect to `a` of gdtr. gdtrib -- Inverse with respect to `b` of gdtr. gdtrix -- Inverse with respect to `x` of gdtr. nbdtr -- Sum of terms 0 through k of the negative binomial pdf. nbdtrc -- Sum of terms k+1 to infinity under negative binomial pdf. nbdtri -- Inverse of nbdtr nbdtrik -- nbdtrin -- ncfdtr -- CDF of non-central t distribution. ncfdtridfd -- Find degrees of freedom (denominator) of noncentral F distribution. ncfdtridfn -- Find degrees of freedom (numerator) of noncentral F distribution. ncfdtri -- Inverse CDF of noncentral F distribution. ncfdtrinc -- Find noncentrality parameter of noncentral F distribution. nctdtr -- CDF of noncentral t distribution. nctdtridf -- Find degrees of freedom of noncentral t distribution. nctdtrit -- Inverse CDF of noncentral t distribution. nctdtrinc -- Find noncentrality parameter of noncentral t distribution. nrdtrimn -- Find mean of normal distribution from cdf and std. nrdtrisd -- Find std of normal distribution from cdf and mean. pdtr -- Sum of terms 0 through k of the Poisson pdf. pdtrc -- Sum of terms k+1 to infinity of the Poisson pdf. pdtri -- Inverse of pdtr pdtrik -- stdtr -- Integral from -infinity to t of the Student-t pdf. stdtridf -- stdtrit -- chdtr -- Integral from 0 to x of the Chi-square pdf. chdtrc -- Integral from x to infnity of Chi-square pdf. chdtri -- Inverse of chdtrc. chdtriv -- ndtr -- Integral from -infinity to x of standard normal pdf log_ndtr -- Logarithm of integral from -infinity to x of standard normal pdf ndtri -- Inverse of ndtr (quantiles) chndtr -- chndtridf -- chndtrinc -- chndtrix -- smirnov -- Kolmogorov-Smirnov complementary CDF for one-sided test statistic (Dn+ or Dn-) smirnovi -- Inverse of smirnov. kolmogorov -- The complementary CDF of the (scaled) two-sided test statistic (Kn*) valid for large n. kolmogi -- Inverse of kolmogorov tklmbda -- Tukey-Lambda CDF logit -- expit -- boxcox -- Compute the Box-Cox transformation. boxcox1p -- Compute the Box-Cox transformation of 1 + x. inv_boxcox -- Compute the inverse of the Box-Cox tranformation. inv_boxcox1p -- Compute the inverse of the Box-Cox transformation of 1 + x. Information Theory Functions ---------------------------- .. autosummary:: :toctree: generated/ entr -- entr(x) = -x*log(x) rel_entr -- rel_entr(x, y) = x*log(x/y) kl_div -- kl_div(x, y) = x*log(x/y) - x + y huber -- Huber loss function. pseudo_huber -- Pseudo-Huber loss function. Gamma and Related Functions --------------------------- .. autosummary:: :toctree: generated/ gamma -- Gamma function. gammaln -- Log of the absolute value of the gamma function. gammasgn -- Sign of the gamma function. gammainc -- Incomplete gamma integral. gammaincinv -- Inverse of gammainc. gammaincc -- Complemented incomplete gamma integral. gammainccinv -- Inverse of gammaincc. beta -- Beta function. betaln -- Log of the absolute value of the beta function. betainc -- Incomplete beta integral. betaincinv -- Inverse of betainc. psi -- Logarithmic derivative of the gamma function. rgamma -- One divided by the gamma function. polygamma -- Nth derivative of psi function. multigammaln -- Log of the multivariate gamma. digamma -- Digamma function (derivative of the logarithm of gamma). poch -- The Pochhammer symbol (rising factorial). Error Function and Fresnel Integrals ------------------------------------ .. autosummary:: :toctree: generated/ erf -- Error function. erfc -- Complemented error function (1- erf(x)) erfcx -- Scaled complemented error function exp(x**2)*erfc(x) erfi -- Imaginary error function, -i erf(i x) erfinv -- Inverse of error function erfcinv -- Inverse of erfc wofz -- Fadeeva function. dawsn -- Dawson's integral. fresnel -- Fresnel sine and cosine integrals. fresnel_zeros -- Complex zeros of both Fresnel integrals modfresnelp -- Modified Fresnel integrals F_+(x) and K_+(x) modfresnelm -- Modified Fresnel integrals F_-(x) and K_-(x) These are not universal functions: .. autosummary:: :toctree: generated/ erf_zeros -- [+]Complex zeros of erf(z) fresnelc_zeros -- [+]Complex zeros of Fresnel cosine integrals fresnels_zeros -- [+]Complex zeros of Fresnel sine integrals Legendre Functions ------------------ .. autosummary:: :toctree: generated/ lpmv -- Associated Legendre Function of arbitrary non-negative degree v. sph_harm -- Spherical Harmonics (complex-valued) Y^m_n(theta,phi) These are not universal functions: .. autosummary:: :toctree: generated/ clpmn -- [+]Associated Legendre Function of the first kind for complex arguments. lpn -- [+]Legendre Functions (polynomials) of the first kind lqn -- [+]Legendre Functions of the second kind. lpmn -- [+]Associated Legendre Function of the first kind for real arguments. lqmn -- [+]Associated Legendre Function of the second kind. Ellipsoidal Harmonics --------------------- .. autosummary:: :toctree: generated/ ellip_harm -- Ellipsoidal harmonic E ellip_harm_2 -- Ellipsoidal harmonic F ellip_normal -- Ellipsoidal normalization constant Orthogonal polynomials ---------------------- The following functions evaluate values of orthogonal polynomials: .. autosummary:: :toctree: generated/ assoc_laguerre eval_legendre eval_chebyt eval_chebyu eval_chebyc eval_chebys eval_jacobi eval_laguerre eval_genlaguerre eval_hermite eval_hermitenorm eval_gegenbauer eval_sh_legendre eval_sh_chebyt eval_sh_chebyu eval_sh_jacobi The functions below, in turn, return the polynomial coefficients in :class:`~.orthopoly1d` objects, which function similarly as :ref:`numpy.poly1d`. The :class:`~.orthopoly1d` class also has an attribute ``weights`` which returns the roots, weights, and total weights for the appropriate form of Gaussian quadrature. These are returned in an ``n x 3`` array with roots in the first column, weights in the second column, and total weights in the final column. Note that :class:`~.orthopoly1d` objects are converted to ``poly1d`` when doing arithmetic, and lose information of the original orthogonal polynomial. .. autosummary:: :toctree: generated/ legendre -- [+]Legendre polynomial P_n(x) (lpn -- for function). chebyt -- [+]Chebyshev polynomial T_n(x) chebyu -- [+]Chebyshev polynomial U_n(x) chebyc -- [+]Chebyshev polynomial C_n(x) chebys -- [+]Chebyshev polynomial S_n(x) jacobi -- [+]Jacobi polynomial P^(alpha,beta)_n(x) laguerre -- [+]Laguerre polynomial, L_n(x) genlaguerre -- [+]Generalized (Associated) Laguerre polynomial, L^alpha_n(x) hermite -- [+]Hermite polynomial H_n(x) hermitenorm -- [+]Normalized Hermite polynomial, He_n(x) gegenbauer -- [+]Gegenbauer (Ultraspherical) polynomials, C^(alpha)_n(x) sh_legendre -- [+]shifted Legendre polynomial, P*_n(x) sh_chebyt -- [+]shifted Chebyshev polynomial, T*_n(x) sh_chebyu -- [+]shifted Chebyshev polynomial, U*_n(x) sh_jacobi -- [+]shifted Jacobi polynomial, J*_n(x) = G^(p,q)_n(x) .. warning:: Computing values of high-order polynomials (around ``order > 20``) using polynomial coefficients is numerically unstable. To evaluate polynomial values, the ``eval_*`` functions should be used instead. Roots and weights for orthogonal polynomials .. autosummary:: :toctree: generated/ c_roots cg_roots h_roots he_roots j_roots js_roots l_roots la_roots p_roots ps_roots s_roots t_roots ts_roots u_roots us_roots Hypergeometric Functions ------------------------ .. autosummary:: :toctree: generated/ hyp2f1 -- Gauss hypergeometric function (2F1) hyp1f1 -- Confluent hypergeometric function (1F1) hyperu -- Confluent hypergeometric function (U) hyp0f1 -- Confluent hypergeometric limit function (0F1) hyp2f0 -- Hypergeometric function (2F0) hyp1f2 -- Hypergeometric function (1F2) hyp3f0 -- Hypergeometric function (3F0) Parabolic Cylinder Functions ---------------------------- .. autosummary:: :toctree: generated/ pbdv -- Parabolic cylinder function Dv(x) and derivative. pbvv -- Parabolic cylinder function Vv(x) and derivative. pbwa -- Parabolic cylinder function W(a,x) and derivative. These are not universal functions: .. autosummary:: :toctree: generated/ pbdv_seq -- [+]Sequence of parabolic cylinder functions Dv(x) pbvv_seq -- [+]Sequence of parabolic cylinder functions Vv(x) pbdn_seq -- [+]Sequence of parabolic cylinder functions Dn(z), complex z Mathieu and Related Functions ----------------------------- .. autosummary:: :toctree: generated/ mathieu_a -- Characteristic values for even solution (ce_m) mathieu_b -- Characteristic values for odd solution (se_m) These are not universal functions: .. autosummary:: :toctree: generated/ mathieu_even_coef -- [+]sequence of expansion coefficients for even solution mathieu_odd_coef -- [+]sequence of expansion coefficients for odd solution The following return both function and first derivative: .. autosummary:: :toctree: generated/ mathieu_cem -- Even Mathieu function mathieu_sem -- Odd Mathieu function mathieu_modcem1 -- Even modified Mathieu function of the first kind mathieu_modcem2 -- Even modified Mathieu function of the second kind mathieu_modsem1 -- Odd modified Mathieu function of the first kind mathieu_modsem2 -- Odd modified Mathieu function of the second kind Spheroidal Wave Functions ------------------------- .. autosummary:: :toctree: generated/ pro_ang1 -- Prolate spheroidal angular function of the first kind pro_rad1 -- Prolate spheroidal radial function of the first kind pro_rad2 -- Prolate spheroidal radial function of the second kind obl_ang1 -- Oblate spheroidal angular function of the first kind obl_rad1 -- Oblate spheroidal radial function of the first kind obl_rad2 -- Oblate spheroidal radial function of the second kind pro_cv -- Compute characteristic value for prolate functions obl_cv -- Compute characteristic value for oblate functions pro_cv_seq -- Compute sequence of prolate characteristic values obl_cv_seq -- Compute sequence of oblate characteristic values The following functions require pre-computed characteristic value: .. autosummary:: :toctree: generated/ pro_ang1_cv -- Prolate spheroidal angular function of the first kind pro_rad1_cv -- Prolate spheroidal radial function of the first kind pro_rad2_cv -- Prolate spheroidal radial function of the second kind obl_ang1_cv -- Oblate spheroidal angular function of the first kind obl_rad1_cv -- Oblate spheroidal radial function of the first kind obl_rad2_cv -- Oblate spheroidal radial function of the second kind Kelvin Functions ---------------- .. autosummary:: :toctree: generated/ kelvin -- All Kelvin functions (order 0) and derivatives. kelvin_zeros -- [+]Zeros of All Kelvin functions (order 0) and derivatives ber -- Kelvin function ber x bei -- Kelvin function bei x berp -- Derivative of Kelvin function ber x beip -- Derivative of Kelvin function bei x ker -- Kelvin function ker x kei -- Kelvin function kei x kerp -- Derivative of Kelvin function ker x keip -- Derivative of Kelvin function kei x These are not universal functions: .. autosummary:: :toctree: generated/ ber_zeros -- [+]Zeros of Kelvin function bei x bei_zeros -- [+]Zeros of Kelvin function ber x berp_zeros -- [+]Zeros of derivative of Kelvin function ber x beip_zeros -- [+]Zeros of derivative of Kelvin function bei x ker_zeros -- [+]Zeros of Kelvin function kei x kei_zeros -- [+]Zeros of Kelvin function ker x kerp_zeros -- [+]Zeros of derivative of Kelvin function ker x keip_zeros -- [+]Zeros of derivative of Kelvin function kei x Combinatorics ------------- .. autosummary:: :toctree: generated/ comb -- [+]Combinations of N things taken k at a time, "N choose k" perm -- [+]Permutations of N things taken k at a time, "k-permutations of N" Other Special Functions ----------------------- .. autosummary:: :toctree: generated/ agm -- Arithmetic-Geometric Mean bernoulli -- Bernoulli numbers binom -- Binomial coefficient. diric -- Dirichlet function (periodic sinc) euler -- Euler numbers expn -- Exponential integral. exp1 -- Exponential integral of order 1 (for complex argument) expi -- Another exponential integral -- Ei(x) factorial -- The factorial function, n! = special.gamma(n+1) factorial2 -- Double factorial, (n!)! factorialk -- [+](...((n!)!)!...)! where there are k '!' shichi -- Hyperbolic sine and cosine integrals. sici -- Integral of the sinc and "cosinc" functions. spence -- Dilogarithm integral. lambertw -- Lambert W function zeta -- Riemann zeta function of two arguments. zetac -- Standard Riemann zeta function minus 1. Convenience Functions --------------------- .. autosummary:: :toctree: generated/ cbrt -- Cube root. exp10 -- 10 raised to the x power. exp2 -- 2 raised to the x power. radian -- radian angle given degrees, minutes, and seconds. cosdg -- cosine of the angle given in degrees. sindg -- sine of the angle given in degrees. tandg -- tangent of the angle given in degrees. cotdg -- cotangent of the angle given in degrees. log1p -- log(1+x) expm1 -- exp(x)-1 cosm1 -- cos(x)-1 round -- round the argument to the nearest integer. If argument ends in 0.5 exactly, pick the nearest even integer. xlogy -- x*log(y) xlog1py -- x*log1p(y) exprel -- (exp(x)-1)/x sinc -- sin(x)/x .. [+] in the description indicates a function which is not a universal .. function and does not follow broadcasting and automatic .. array-looping rules. """
""" Mongo-based Experiment driver and worker client =============================================== Components involved: - mongo e.g. mongod ... - driver e.g. hyperopt-mongo-search mongo://address bandit_json bandit_algo_json - worker e.g. hyperopt-mongo-worker --loop mongo://address Mongo ===== Mongo (daemon process mongod) is used for IPC between the driver and worker. Configure it as you like, so that hyperopt-mongo-search can communicate with it. I think there is some support in this file for an ssh+mongo connection type. The experiment uses the following collections for IPC: * jobs - documents of a standard form used to store suggested trials and their results. These documents have keys: * spec : subdocument returned by bandit_algo.suggest * exp_key: an identifier of which driver suggested this trial * cmd: a tuple (protocol, ...) identifying bandit.evaluate * state: 0, 1, 2, 3 for job state (new, running, ok, fail) * owner: None for new jobs, (hostname, pid) for started jobs * book_time: time a job was reserved * refresh_time: last time the process running the job checked in * result: the subdocument returned by bandit.evaluate * error: for jobs of state 3, a reason for failure. * logs: a dict of sequences of strings received by ctrl object * info: info messages * warn: warning messages * error: error messages * fs - a gridfs storage collection (used for pickling) * drivers - documents describing drivers. These are used to prevent two drivers from using the same exp_key simultaneously, and to attach saved states. * exp_key * workdir: [optional] path where workers should chdir to Attachments: * pkl: [optional] saved state of experiment class * bandit_args_kwargs: [optional] pickled (clsname, args, kwargs) to reconstruct bandit in worker processes The MongoJobs, MongoExperiment, and CtrlObj classes as well as the main_worker method form the abstraction barrier around this database layout. Driver ====== A driver directs an experiment, by calling a bandit_algo to suggest trial points, and queuing them in mongo so that a worker can evaluate that trial point. The hyperopt-mongo-search script creates a single MongoExperiment instance, and calls its run() method. Saving and Resuming ------------------- The command "hyperopt-mongo-search bandit algo" creates a new experiment or resumes an existing experiment. The command "hyperopt-mongo-search --exp-key=<EXPKEY>" can only resume an existing experiment. The command "hyperopt-mongo-search --clear-existing bandit algo" can only create a new experiment, and potentially deletes an existing one. The command "hyperopt-mongo-search --clear-existing --exp-key=EXPKEY bandit algo" can only create a new experiment, and potentially deletes an existing one. By default, MongoExperiment.run will try to save itself before returning. It does so by pickling itself to a file called 'exp_key' in the fs collection. Resuming means unpickling that file and calling run again. The MongoExperiment instance itself is minimal (a key, a bandit, a bandit algo, a workdir, a poll interval). The only stateful element is the bandit algo. The difference between resume and start is in the handling of the bandit algo. Worker ====== A worker looks up a job in a mongo database, maps that job document to a runnable python object, calls that object, and writes the return value back to the database. A worker *reserves* a job by atomically identifying a document in the jobs collection whose owner is None and whose state is 0, and setting the state to 1. If it fails to identify such a job, it loops with a random sleep interval of a few seconds and polls the database. If hyperopt-mongo-worker is called with a --loop argument then it goes back to the database after finishing a job to identify and perform another one. CtrlObj ------- The worker allocates a CtrlObj and passes it to bandit.evaluate in addition to the subdocument found at job['spec']. A bandit can use ctrl.info, ctrl.warn, ctrl.error and so on like logger methods, and those messages will be written to the mongo database (to job['logs']). They are not written synchronously though, they are written when the bandit.evaluate function calls ctrl.checkpoint(). Ctrl.checkpoint does several things: * flushes logging messages to the database * updates the refresh_time * optionally updates the result subdocument The main_worker routine calls Ctrl.checkpoint(rval) once after the bandit.evalute function has returned before setting the state to 2 or 3 to finalize the job in the database. """
"""Stuff to parse AIFF-C and AIFF files. Unless explicitly stated otherwise, the description below is true both for AIFF-C files and AIFF files. An AIFF-C file has the following structure. +-----------------+ | FORM | +-----------------+ | <size> | +----+------------+ | | AIFC | | +------------+ | | <chunks> | | | . | | | . | | | . | +----+------------+ An AIFF file has the string "AIFF" instead of "AIFC". A chunk consists of an identifier (4 bytes) followed by a size (4 bytes, big endian order), followed by the data. The size field does not include the size of the 8 byte header. The following chunk types are recognized. FVER <version number of AIFF-C defining document> (AIFF-C only). MARK <# of markers> (2 bytes) list of markers: <marker ID> (2 bytes, must be > 0) <position> (4 bytes) <marker name> ("pstring") COMM <# of channels> (2 bytes) <# of sound frames> (4 bytes) <size of the samples> (2 bytes) <sampling frequency> (10 bytes, IEEE 80-bit extended floating point) in AIFF-C files only: <compression type> (4 bytes) <human-readable version of compression type> ("pstring") SSND <offset> (4 bytes, not used by this program) <blocksize> (4 bytes, not used by this program) <sound data> A pstring consists of 1 byte length, a string of characters, and 0 or 1 byte pad to make the total length even. Usage. Reading AIFF files: f = aifc.open(file, 'r') where file is either the name of a file or an open file pointer. The open file pointer must have methods read(), seek(), and close(). In some types of audio files, if the setpos() method is not used, the seek() method is not necessary. This returns an instance of a class with the following public methods: getnchannels() -- returns number of audio channels (1 for mono, 2 for stereo) getsampwidth() -- returns sample width in bytes getframerate() -- returns sampling frequency getnframes() -- returns number of audio frames getcomptype() -- returns compression type ('NONE' for AIFF files) getcompname() -- returns human-readable version of compression type ('not compressed' for AIFF files) getparams() -- returns a tuple consisting of all of the above in the above order getmarkers() -- get the list of marks in the audio file or None if there are no marks getmark(id) -- get mark with the specified id (raises an error if the mark does not exist) readframes(n) -- returns at most n frames of audio rewind() -- rewind to the beginning of the audio stream setpos(pos) -- seek to the specified position tell() -- return the current position close() -- close the instance (make it unusable) The position returned by tell(), the position given to setpos() and the position of marks are all compatible and have nothing to do with the actual position in the file. The close() method is called automatically when the class instance is destroyed. Writing AIFF files: f = aifc.open(file, 'w') where file is either the name of a file or an open file pointer. The open file pointer must have methods write(), tell(), seek(), and close(). This returns an instance of a class with the following public methods: aiff() -- create an AIFF file (AIFF-C default) aifc() -- create an AIFF-C file setnchannels(n) -- set the number of channels setsampwidth(n) -- set the sample width setframerate(n) -- set the frame rate setnframes(n) -- set the number of frames setcomptype(type, name) -- set the compression type and the human-readable compression type setparams(tuple) -- set all parameters at once setmark(id, pos, name) -- add specified mark to the list of marks tell() -- return current position in output file (useful in combination with setmark()) writeframesraw(data) -- write audio frames without pathing up the file header writeframes(data) -- write audio frames and patch up the file header close() -- patch up the file header and close the output file You should set the parameters before the first writeframesraw or writeframes. The total number of frames does not need to be set, but when it is set to the correct value, the header does not have to be patched up. It is best to first set all parameters, perhaps possibly the compression type, and then write audio frames using writeframesraw. When all frames have been written, either call writeframes('') or close() to patch up the sizes in the header. Marks can be added anytime. If there are any marks, ypu must call close() after all frames have been written. The close() method is called automatically when the class instance is destroyed. When a file is opened with the extension '.aiff', an AIFF file is written, otherwise an AIFF-C file is written. This default can be changed by calling aiff() or aifc() before the first writeframes or writeframesraw. """
# FAILED ATTEMPT AT REFORMING MOLECULESTUB # Perhaps I'll come back to this later... #from grendel import type_checking_enabled, sanity_checking_enabled #from grendel.chemistry.atom import Atom #from grendel.gmath import magnitude, angle_between_vectors #from grendel.gmath.matrix import Matrix #from grendel.util.decorators import with_flexible_arguments, typechecked, IterableOf #from grendel.util.exceptions import ChemistryError #from grendel.util.overloading import overloaded, OverloadedFunctionCallError #from grendel.util.strings import indented #from grendel.util.units import strip_units, DistanceUnit, AngularUnit, Radians, Degrees, Angstroms, isunit ## Immutable "friend class" of Molecule #class MoleculeStub(object): # """ # Immutable "friend" class of `Molecule`, used for hashing. # """ # # #################### # # Class Attributes # # #################### # # eq_precision = 8 # same_internal_tol = {AngularUnit: 0.0001*Degrees, DistanceUnit: 1e-7*Angstroms} # # ############## # # Attributes # # ############## # # multiplicity = None # """ The multiplicity of the electronic state of the molecule. # (i.e. 2S+1 where S is the total spin). Defaults to singlet. """ # # charge = None # """ The charge on the molecule. Defaults to neutral. """ # # reoriented_matrix = None # # ###################### # # Private Attributes # # ###################### # # _hash = None # _cartesian_representation = None # _cartesian_units = None # _internal_representation = None # _from_molecule = None # _xyz = None # _element_list = None # # ################## # # Initialization # # ################## # # @overloaded # def __init__(self, *args, **kwargs): # raise OverloadedFunctionCallError # # @__init__.overload_with( # atoms=IterableOf('Atom'), # ) # def __init__(self, # atoms, # **kwargs): # self.__init__( # [(atom.element, atom.isotope) for atom in atoms], # Matrix([atom.position for atom in atoms]), # **kwargs) # # @__init__.overload_with( # cartesian_units=isunit, # charge=(int, None), # multiplicity=(int, None) # ) # def __init__(self, # elements_and_isotopes, # xyz, # cartesian_units=DistanceUnit.default, # charge=None, # multiplicity=None): # self._cartesian_units = cartesian_units # self.charge = charge if charge is not None else Molecule.default_charge # self.multiplicity = multiplicity if multiplicity is not None else Molecule.default_multiplicity # self._xyz = xyz # self._element_list = elements_and_isotopes # # TODO strip units # tmpmol = Molecule( # [Atom(el, iso, pos) for (el, iso), pos in zip(self._element_list, self._xyz.iter_rows)], # charge=self.charge, # multiplicity=self.multiplicity # ) # self.reoriented_matrix = tmpmol.reoriented().xyz # # ################### # # Special Methods # # ################### # # def __hash__(self): # if self._hash is not None: # return self._hash # self._hash = MoleculeDict.hash_for(self) # return self._hash # # def __eq__(self, other): # if isinstance(other, MoleculeStub): # if [a.isotope for a in self] != [a.isotope for a in other]: # return False # elif (self.multiplicity, self.charge) != (other.multiplicity, other.charge): # return False # else: # reoriented = self.reoriented_matrix * self._cartesian_units.to(DistanceUnit.default) # rounded = [round(v, MoleculeStub.eq_precision) for v in reoriented.ravel()] # other_oriented = other.reoriented_matrix * other._cartesian_units.to(DistanceUnit.default) # other_rounded = [round(v, MoleculeStub.eq_precision) for v in other_oriented.ravel()] # return rounded == other_rounded # else: # return NotImplemented # # # ########### # # Methods # # ########### # # # TODO document this! # # TODO class variables for default tolerances # def is_valid_stub_for(self, other, cart_tol=None, internal_tol=None, ang_tol=None): # """ # """ # # cart_tol is used for comparison between cartesian positions # cart_tol = cart_tol or 1e-8*Angstroms # # internal_tol should be unitless, since the difference between internal coordinates could have multiple # # units, and we're taking the magnitude across these units # internal_tol = internal_tol or MoleculeStub.same_internal_tol # # ang_tol is used when comparing cartesian geometries. If the angle between two corresponding atoms # # in self and other differs from the angle between the first two corresponding atoms in self and other # # by more than ang_tol, we assume they do not have the same geometry and thus return False # ang_tol = ang_tol or 1e-5*Degrees # #--------------------------------------------------------------------------------# # if type_checking_enabled: # if not isinstance(other, Molecule): # raise TypeError # if isinstance(other, MoleculeStub): # raise TypeError # #--------------------------------------------------------------------------------# # if self.multiplicity != other.multiplicity: # return False # elif self.charge != other.charge: # return False # else: # if len(self._element_list) != other.natoms: # for num, (element, isotope) in enumerate(self._element_list): # if (other[num].element, other[num].isotope) != (element, isotope): # return False # if other.natoms <= 1: # # if we have 1 or 0 atoms and we've gotten this far, we have a match # return True # #--------------------------------------------------------------------------------# # # no failures yet, so we have to compare geometries # # if self has an internal_representation, use it # if self._internal_representation is not None: # diff = self._internal_representation.values - self._internal_representation.values_for_molecule(other) # if any(abs(d) > internal_tol[c.units.genre].in_units(c.units) for d, c in zip(diff, self._internal_representation)): # return False # return True # # if mol has an internal representation, use it: # elif other.internal_representation is not None: # diff = other.internal_representation.values - other.internal_representation.values_for_molecule(self) # if any(abs(d) > internal_tol[c.units.genre].in_units(c.units) for d, c in zip(diff, other.internal_representation)): # return False # return True # else: # # They're both fully cartesian. This could take a while... # # We should first try to short-circuit as many ways as possible # #----------------------------------------# # # first strip units and store stripped versions to speed up the rest of the work # # strip the units off of ang_tol # ang_tol = strip_units(ang_tol, Radians) # # strip the units off of cart_tol # cart_tol = strip_units(cart_tol, self._cartesian_units) # # make a list of positions with stripped units, since we'll use it up to three times # stripped = [strip_units(atom, self._cartesian_units) for atom in (self if self.is_centered() else self.recentered()) ] # other_stripped = [strip_units(atom, self._cartesian_units) for atom in (other if other.is_centered() else other.recentered())] # #----------------------------------------# # # Try to short-circuit negatively by looking for an inconsistancy in the angles # # between pairs of corresponding atoms # # If the first atom is at the origin, use the second one # offset = 1 # if stripped[0].is_zero(): # if magnitude(stripped[0] - other_stripped[0]) > cart_tol: # return False # else: # if sanity_checking_enabled and stripped[1].is_zero(): # raise ChemistryError, "FrozenMolecule:\n{}\nhas two atoms on top of each other.".format(indented(str(self))) # if other_stripped[1].is_zero(): # return False # else: # offset = 2 # first_ang = angle_between_vectors(self.atoms[1].pos, other.atoms[1].pos) # else: # if other_stripped[0].is_zero(): # return False # else: # first_ang = angle_between_vectors(self.atoms[0].pos, other.atoms[0].pos) # for apos, opos in zip(stripped[offset:], other_stripped[offset:]): # if apos.is_zero(): # if magnitude(apos - opos) > cart_tol: # return False # elif opos.is_zero(): # # Try again, since Tensor.zero_cutoff could smaller than cart_tol, causing a false zero # if magnitude(apos - opos) > cart_tol: # return False # else: # ang = angle_between_vectors(apos, opos) # if abs(ang - first_ang) > ang_tol: # return False # # Also, the magnitude of the distance from the center of mass should be the same: # if abs(apos.magnitude() - opos.magnitude()) > cart_tol: # return False # #----------------------------------------# # # Try to short-circuit positively: # exact_match = True # for apos, opos in zip(stripped, other_stripped): # if magnitude(apos - opos) > cart_tol: # exact_match = False # break # if exact_match: # return True # exact_match = True # # Check negative version # for apos, opos in zip(stripped, other_stripped): # if magnitude(apos + opos) > cart_tol: # exact_match = False # break # if exact_match: # return True # #----------------------------------------# # # We can't short-circuit, so this is the only means we have left # # It's far more expensive than the rest, but it always works. # return self.has_same_geometry(other, cart_tol) # # ###################### ## Dependent Imports # ###################### # #from grendel.chemistry.molecule_dict import MoleculeDict #from grendel.chemistry.molecule import Molecule
#!/usr/bin/env python # -*- coding: utf-8 -*- ## GNU GENERAL PUBLIC LICENSE ## Version 2, June 1991 ## Copyright (C) 1989, 1991 Free Software Foundation, Inc. ## 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## Everyone is permitted to copy and distribute verbatim copies ## of this license document, but changing it is not allowed. ## Preamble ## The licenses for most software are designed to take away your ##freedom to share and change it. By contrast, the GNU General Public ##License is intended to guarantee your freedom to share and change free ##software--to make sure the software is free for all its users. This ##General Public License applies to most of the Free Software ##Foundation's software and to any other program whose authors commit to ##using it. (Some other Free Software Foundation software is covered by ##the GNU Library General Public License instead.) You can apply it to ##your programs, too. ## When we speak of free software, we are referring to freedom, not ##price. Our General Public Licenses are designed to make sure that you ##have the freedom to distribute copies of free software (and charge for ##this service if you wish), that you receive source code or can get it ##if you want it, that you can change the software or use pieces of it ##in new free programs; and that you know you can do these things. ## To protect your rights, we need to make restrictions that forbid ##anyone to deny you these rights or to ask you to surrender the rights. ##These restrictions translate to certain responsibilities for you if you ##distribute copies of the software, or if you modify it. ## For example, if you distribute copies of such a program, whether ##gratis or for a fee, you must give the recipients all the rights that ##you have. You must make sure that they, too, receive or can get the ##source code. And you must show them these terms so they know their ##rights. ## We protect your rights with two steps: (1) copyright the software, and ##(2) offer you this license which gives you legal permission to copy, ##distribute and/or modify the software. ## Also, for each author's protection and ours, we want to make certain ##that everyone understands that there is no warranty for this free ##software. If the software is modified by someone else and passed on, we ##want its recipients to know that what they have is not the original, so ##that any problems introduced by others will not reflect on the original ##authors' reputations. ## Finally, any free program is threatened constantly by software ##patents. We wish to avoid the danger that redistributors of a free ##program will individually obtain patent licenses, in effect making the ##program proprietary. To prevent this, we have made it clear that any ##patent must be licensed for everyone's free use or not licensed at all. ## The precise terms and conditions for copying, distribution and ##modification follow. ## GNU GENERAL PUBLIC LICENSE ## TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION ## 0. This License applies to any program or other work which contains ##a notice placed by the copyright holder saying it may be distributed ##under the terms of this General Public License. The "Program", below, ##refers to any such program or work, and a "work based on the Program" ##means either the Program or any derivative work under copyright law: ##that is to say, a work containing the Program or a portion of it, ##either verbatim or with modifications and/or translated into another ##language. (Hereinafter, translation is included without limitation in ##the term "modification".) Each licensee is addressed as "you". ##Activities other than copying, distribution and modification are not ##covered by this License; they are outside its scope. The act of ##running the Program is not restricted, and the output from the Program ##is covered only if its contents constitute a work based on the ##Program (independent of having been made by running the Program). ##Whether that is true depends on what the Program does. ## 1. You may copy and distribute verbatim copies of the Program's ##source code as you receive it, in any medium, provided that you ##conspicuously and appropriately publish on each copy an appropriate ##copyright notice and disclaimer of warranty; keep intact all the ##notices that refer to this License and to the absence of any warranty; ##and give any other recipients of the Program a copy of this License ##along with the Program. ##You may charge a fee for the physical act of transferring a copy, and ##you may at your option offer warranty protection in exchange for a fee. ## 2. You may modify your copy or copies of the Program or any portion ##of it, thus forming a work based on the Program, and copy and ##distribute such modifications or work under the terms of Section 1 ##above, provided that you also meet all of these conditions: ## a) You must cause the modified files to carry prominent notices ## stating that you changed the files and the date of any change. ## b) You must cause any work that you distribute or publish, that in ## whole or in part contains or is derived from the Program or any ## part thereof, to be licensed as a whole at no charge to all third ## parties under the terms of this License. ## c) If the modified program normally reads commands interactively ## when run, you must cause it, when started running for such ## interactive use in the most ordinary way, to print or display an ## announcement including an appropriate copyright notice and a ## notice that there is no warranty (or else, saying that you provide ## a warranty) and that users may redistribute the program under ## these conditions, and telling the user how to view a copy of this ## License. (Exception: if the Program itself is interactive but ## does not normally print such an announcement, your work based on ## the Program is not required to print an announcement.) ##These requirements apply to the modified work as a whole. If ##identifiable sections of that work are not derived from the Program, ##and can be reasonably considered independent and separate works in ##themselves, then this License, and its terms, do not apply to those ##sections when you distribute them as separate works. But when you ##distribute the same sections as part of a whole which is a work based ##on the Program, the distribution of the whole must be on the terms of ##this License, whose permissions for other licensees extend to the ##entire whole, and thus to each and every part regardless of who wrote it. ##Thus, it is not the intent of this section to claim rights or contest ##your rights to work written entirely by you; rather, the intent is to ##exercise the right to control the distribution of derivative or ##collective works based on the Program. ##In addition, mere aggregation of another work not based on the Program ##with the Program (or with a work based on the Program) on a volume of ##a storage or distribution medium does not bring the other work under ##the scope of this License. ## 3. You may copy and distribute the Program (or a work based on it, ##under Section 2) in object code or executable form under the terms of ##Sections 1 and 2 above provided that you also do one of the following: ## a) Accompany it with the complete corresponding machine-readable ## source code, which must be distributed under the terms of Sections ## 1 and 2 above on a medium customarily used for software interchange; or, ## b) Accompany it with a written offer, valid for at least three ## years, to give any third party, for a charge no more than your ## cost of physically performing source distribution, a complete ## machine-readable copy of the corresponding source code, to be ## distributed under the terms of Sections 1 and 2 above on a medium ## customarily used for software interchange; or, ## c) Accompany it with the information you received as to the offer ## to distribute corresponding source code. (This alternative is ## allowed only for noncommercial distribution and only if you ## received the program in object code or executable form with such ## an offer, in accord with Subsection b above.) ##The source code for a work means the preferred form of the work for ##making modifications to it. For an executable work, complete source ##code means all the source code for all modules it contains, plus any ##associated interface definition files, plus the scripts used to ##control compilation and installation of the executable. However, as a ##special exception, the source code distributed need not include ##anything that is normally distributed (in either source or binary ##form) with the major components (compiler, kernel, and so on) of the ##operating system on which the executable runs, unless that component ##itself accompanies the executable. ##If distribution of executable or object code is made by offering ##access to copy from a designated place, then offering equivalent ##access to copy the source code from the same place counts as ##distribution of the source code, even though third parties are not ##compelled to copy the source along with the object code. ## 4. You may not copy, modify, sublicense, or distribute the Program ##except as expressly provided under this License. Any attempt ##otherwise to copy, modify, sublicense or distribute the Program is ##void, and will automatically terminate your rights under this License. ##However, parties who have received copies, or rights, from you under ##this License will not have their licenses terminated so long as such ##parties remain in full compliance. ## 5. You are not required to accept this License, since you have not ##signed it. However, nothing else grants you permission to modify or ##distribute the Program or its derivative works. These actions are ##prohibited by law if you do not accept this License. Therefore, by ##modifying or distributing the Program (or any work based on the ##Program), you indicate your acceptance of this License to do so, and ##all its terms and conditions for copying, distributing or modifying ##the Program or works based on it. ## 6. Each time you redistribute the Program (or any work based on the ##Program), the recipient automatically receives a license from the ##original licensor to copy, distribute or modify the Program subject to ##these terms and conditions. You may not impose any further ##restrictions on the recipients' exercise of the rights granted herein. ##You are not responsible for enforcing compliance by third parties to ##this License. ## 7. If, as a consequence of a court judgment or allegation of patent ##infringement or for any other reason (not limited to patent issues), ##conditions are imposed on you (whether by court order, agreement or ##otherwise) that contradict the conditions of this License, they do not ##excuse you from the conditions of this License. If you cannot ##distribute so as to satisfy simultaneously your obligations under this ##License and any other pertinent obligations, then as a consequence you ##may not distribute the Program at all. For example, if a patent ##license would not permit royalty-free redistribution of the Program by ##all those who receive copies directly or indirectly through you, then ##the only way you could satisfy both it and this License would be to ##refrain entirely from distribution of the Program. ##If any portion of this section is held invalid or unenforceable under ##any particular circumstance, the balance of the section is intended to ##apply and the section as a whole is intended to apply in other ##circumstances. ##It is not the purpose of this section to induce you to infringe any ##patents or other property right claims or to contest validity of any ##such claims; this section has the sole purpose of protecting the ##integrity of the free software distribution system, which is ##implemented by public license practices. Many people have made ##generous contributions to the wide range of software distributed ##through that system in reliance on consistent application of that ##system; it is up to the author/donor to decide if he or she is willing ##to distribute software through any other system and a licensee cannot ##impose that choice. ##This section is intended to make thoroughly clear what is believed to ##be a consequence of the rest of this License. ## 8. If the distribution and/or use of the Program is restricted in ##certain countries either by patents or by copyrighted interfaces, the ##original copyright holder who places the Program under this License ##may add an explicit geographical distribution limitation excluding ##those countries, so that distribution is permitted only in or among ##countries not thus excluded. In such case, this License incorporates ##the limitation as if written in the body of this License. ## 9. The Free Software Foundation may publish revised and/or new versions ##of the General Public License from time to time. Such new versions will ##be similar in spirit to the present version, but may differ in detail to ##address new problems or concerns. ##Each version is given a distinguishing version number. If the Program ##specifies a version number of this License which applies to it and "any ##later version", you have the option of following the terms and conditions ##either of that version or of any later version published by the Free ##Software Foundation. If the Program does not specify a version number of ##this License, you may choose any version ever published by the Free Software ##Foundation. ## 10. If you wish to incorporate parts of the Program into other free ##programs whose distribution conditions are different, write to the author ##to ask for permission. For software which is copyrighted by the Free ##Software Foundation, write to the Free Software Foundation; we sometimes ##make exceptions for this. Our decision will be guided by the two goals ##of preserving the free status of all derivatives of our free software and ##of promoting the sharing and reuse of software generally. ## NO WARRANTY ## 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY ##FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN ##OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES ##PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED ##OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF ##MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS ##TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE ##PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, ##REPAIR OR CORRECTION. ## 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING ##WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR ##REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, ##INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING ##OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED ##TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY ##YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER ##PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE ##POSSIBILITY OF SUCH DAMAGES. ## END OF TERMS AND CONDITIONS ##Copyright (C) [2003] [Jürgen NAME D-32584 Löhne] ##This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as ##published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. ##This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied ##warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ##for more details. ##You should have received a copy of the GNU General Public License along with this program; if not, write to the ##Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
# from trex import redis # # from .mixins import REDIS_HOST, REDIS_PORT # # # class TestRedisConnections(unittest.TestCase): # _KEYS = ['trex:testwatch1', 'trex:testwatch2'] # # @defer.inlineCallbacks # def setUp(self): # self.connections = [] # self.db = yield self._getRedisConnection() # yield self.db.delete(self._KEYS) # # @defer.inlineCallbacks # def tearDown(self): # for connection in self.connections: # l = [connection.delete(k) for k in self._KEYS] # yield defer.DeferredList(l) # yield connection.disconnect() # # def _db_connected(self, connection): # self.connections.append(connection) # return connection # # def _getRedisConnection(self, host=REDIS_HOST, port=REDIS_PORT, db=0): # return redis.Connection( # host, port, dbid=db, reconnect=False).addCallback( # self._db_connected) # # def _check_watcherror(self, response, shouldError=False): # if shouldError: # self.assertIsInstance(response, Failure) # self.assertIsInstance(response.value, redis.WatchError) # else: # self.assertNotIsInstance(response, Failure) # # @defer.inlineCallbacks # def testRedisWatchFail(self): # db1 = yield self._getRedisConnection() # yield self.db.set(self._KEYS[0], 'foo') # t = yield self.db.multi(self._KEYS[0]) # self.assertIsInstance(t, redis.RedisProtocol) # yield t.set(self._KEYS[1], 'bar') # # This should trigger a failure # yield db1.set(self._KEYS[0], 'bar1') # yield t.commit().addBoth(self._check_watcherror, shouldError=True) # # @defer.inlineCallbacks # def testRedisWatchSucceed(self): # yield self.db.set(self._KEYS[0], 'foo') # t = yield self.db.multi(self._KEYS[0]) # self.assertIsInstance(t, redis.RedisProtocol) # yield t.set(self._KEYS[0], 'bar') # yield t.commit().addBoth(self._check_watcherror, shouldError=False) # # @defer.inlineCallbacks # def testRedisMultiNoArgs(self): # yield self.db.set(self._KEYS[0], 'foo') # t = yield self.db.multi() # self.assertIsInstance(t, redis.RedisProtocol) # yield t.set(self._KEYS[1], 'bar') # yield t.commit().addBoth(self._check_watcherror, shouldError=False) # # @defer.inlineCallbacks # def testRedisWithBulkCommands_transactions(self): # t = yield self.db.watch(self._KEYS) # yield t.mget(self._KEYS) # t = yield t.multi() # yield t.commit() # self.assertEqual(0, t.transactions) # self.assertFalse(t.inTransaction) # # @defer.inlineCallbacks # def testRedisWithBulkCommands_inTransaction(self): # t = yield self.db.watch(self._KEYS) # yield t.mget(self._KEYS) # self.assertTrue(t.inTransaction) # yield t.unwatch() # # @defer.inlineCallbacks # def testRedisWithBulkCommands_mget(self): # yield self.db.set(self._KEYS[0], "foo") # yield self.db.set(self._KEYS[1], "bar") # # m0 = yield self.db.mget(self._KEYS) # t = yield self.db.watch(self._KEYS) # m1 = yield t.mget(self._KEYS) # t = yield t.multi() # yield t.mget(self._KEYS) # (m2,) = yield t.commit() # # self.assertEqual(["foo", "bar"], m0) # self.assertEqual(m0, m1) # self.assertEqual(m0, m2) # # @defer.inlineCallbacks # def testRedisWithBulkCommands_hgetall(self): # yield self.db.hset(self._KEYS[0], "foo", "bar") # yield self.db.hset(self._KEYS[0], "bar", "foo") # # h0 = yield self.db.hgetall(self._KEYS[0]) # t = yield self.db.watch(self._KEYS[0]) # h1 = yield t.hgetall(self._KEYS[0]) # t = yield t.multi() # yield t.hgetall(self._KEYS[0]) # (h2,) = yield t.commit() # # self.assertEqual({"foo": "bar", "bar": "foo"}, h0) # self.assertEqual(h0, h1) # self.assertEqual(h0, h2)
""" ============ Array basics ============ Array types and conversions between types ========================================= NumPy supports a much greater variety of numerical types than Python does. This section shows which are available, and how to modify an array's data-type. ========== ========================================================== Data type Description ========== ========================================================== bool_ Boolean (True or False) stored as a byte int_ Default integer type (same as C ``long``; normally either ``int64`` or ``int32``) intc Identical to C ``int`` (normally ``int32`` or ``int64``) intp Integer used for indexing (same as C ``ssize_t``; normally either ``int32`` or ``int64``) int8 Byte (-128 to 127) int16 Integer (-32768 to 32767) int32 Integer (-2147483648 to 2147483647) int64 Integer (-9223372036854775808 to 9223372036854775807) uint8 Unsigned integer (0 to 255) uint16 Unsigned integer (0 to 65535) uint32 Unsigned integer (0 to 4294967295) uint64 Unsigned integer (0 to 18446744073709551615) float_ Shorthand for ``float64``. float16 Half precision float: sign bit, 5 bits exponent, 10 bits mantissa float32 Single precision float: sign bit, 8 bits exponent, 23 bits mantissa float64 Double precision float: sign bit, 11 bits exponent, 52 bits mantissa complex_ Shorthand for ``complex128``. complex64 Complex number, represented by two 32-bit floats (real and imaginary components) complex128 Complex number, represented by two 64-bit floats (real and imaginary components) ========== ========================================================== Additionally to ``intc`` the platform dependent C integer types ``short``, ``long``, ``longlong`` and their unsigned versions are defined. NumPy numerical types are instances of ``dtype`` (data-type) objects, each having unique characteristics. Once you have imported NumPy using :: >>> import numpy as np the dtypes are available as ``np.bool_``, ``np.float32``, etc. Advanced types, not listed in the table above, are explored in section :ref:`structured_arrays`. There are 5 basic numerical types representing booleans (bool), integers (int), unsigned integers (uint) floating point (float) and complex. Those with numbers in their name indicate the bitsize of the type (i.e. how many bits are needed to represent a single value in memory). Some types, such as ``int`` and ``intp``, have differing bitsizes, dependent on the platforms (e.g. 32-bit vs. 64-bit machines). This should be taken into account when interfacing with low-level code (such as C or Fortran) where the raw memory is addressed. Data-types can be used as functions to convert python numbers to array scalars (see the array scalar section for an explanation), python sequences of numbers to arrays of that type, or as arguments to the dtype keyword that many numpy functions or methods accept. Some examples:: >>> import numpy as np >>> x = np.float32(1.0) >>> x 1.0 >>> y = np.int_([1,2,4]) >>> y array([1, 2, 4]) >>> z = np.arange(3, dtype=np.uint8) >>> z array([0, 1, 2], dtype=uint8) Array types can also be referred to by character codes, mostly to retain backward compatibility with older packages such as Numeric. Some documentation may still refer to these, for example:: >>> np.array([1, 2, 3], dtype='f') array([ 1., 2., 3.], dtype=float32) We recommend using dtype objects instead. To convert the type of an array, use the .astype() method (preferred) or the type itself as a function. For example: :: >>> z.astype(float) #doctest: +NORMALIZE_WHITESPACE array([ 0., 1., 2.]) >>> np.int8(z) array([0, 1, 2], dtype=int8) Note that, above, we use the *Python* float object as a dtype. NumPy knows that ``int`` refers to ``np.int_``, ``bool`` means ``np.bool_``, that ``float`` is ``np.float_`` and ``complex`` is ``np.complex_``. The other data-types do not have Python equivalents. To determine the type of an array, look at the dtype attribute:: >>> z.dtype dtype('uint8') dtype objects also contain information about the type, such as its bit-width and its byte-order. The data type can also be used indirectly to query properties of the type, such as whether it is an integer:: >>> d = np.dtype(int) >>> d dtype('int32') >>> np.issubdtype(d, int) True >>> np.issubdtype(d, float) False Array Scalars ============= NumPy generally returns elements of arrays as array scalars (a scalar with an associated dtype). Array scalars differ from Python scalars, but for the most part they can be used interchangeably (the primary exception is for versions of Python older than v2.x, where integer array scalars cannot act as indices for lists and tuples). There are some exceptions, such as when code requires very specific attributes of a scalar or when it checks specifically whether a value is a Python scalar. Generally, problems are easily fixed by explicitly converting array scalars to Python scalars, using the corresponding Python type function (e.g., ``int``, ``float``, ``complex``, ``str``, ``unicode``). The primary advantage of using array scalars is that they preserve the array type (Python may not have a matching scalar type available, e.g. ``int16``). Therefore, the use of array scalars ensures identical behaviour between arrays and scalars, irrespective of whether the value is inside an array or not. NumPy scalars also have many of the same methods arrays do. Extended Precision ================== Python's floating-point numbers are usually 64-bit floating-point numbers, nearly equivalent to ``np.float64``. In some unusual situations it may be useful to use floating-point numbers with more precision. Whether this is possible in numpy depends on the hardware and on the development environment: specifically, x86 machines provide hardware floating-point with 80-bit precision, and while most C compilers provide this as their ``long double`` type, MSVC (standard for Windows builds) makes ``long double`` identical to ``double`` (64 bits). NumPy makes the compiler's ``long double`` available as ``np.longdouble`` (and ``np.clongdouble`` for the complex numbers). You can find out what your numpy provides with``np.finfo(np.longdouble)``. NumPy does not provide a dtype with more precision than C ``long double``s; in particular, the 128-bit IEEE quad precision data type (FORTRAN's ``REAL*16``) is not available. For efficient memory alignment, ``np.longdouble`` is usually stored padded with zero bits, either to 96 or 128 bits. Which is more efficient depends on hardware and development environment; typically on 32-bit systems they are padded to 96 bits, while on 64-bit systems they are typically padded to 128 bits. ``np.longdouble`` is padded to the system default; ``np.float96`` and ``np.float128`` are provided for users who want specific padding. In spite of the names, ``np.float96`` and ``np.float128`` provide only as much precision as ``np.longdouble``, that is, 80 bits on most x86 machines and 64 bits in standard Windows builds. Be warned that even if ``np.longdouble`` offers more precision than python ``float``, it is easy to lose that extra precision, since python often forces values to pass through ``float``. For example, the ``%`` formatting operator requires its arguments to be converted to standard python types, and it is therefore impossible to preserve extended precision even if many decimal places are requested. It can be useful to test your code with the value ``1 + np.finfo(np.longdouble).eps``. """
"""Configuration file parser. A configuration file consists of sections, lead by a "[section]" header, and followed by "name: value" entries, with continuations and such in the style of RFC 822. Intrinsic defaults can be specified by passing them into the ConfigParser constructor as a dictionary. class: ConfigParser -- responsible for parsing a list of configuration files, and managing the parsed database. methods: __init__(defaults=None, dict_type=_default_dict, allow_no_value=False, delimiters=('=', ':'), comment_prefixes=('#', ';'), inline_comment_prefixes=None, strict=True, empty_lines_in_values=True): Create the parser. When `defaults' is given, it is initialized into the dictionary or intrinsic defaults. The keys must be strings, the values must be appropriate for %()s string interpolation. When `dict_type' is given, it will be used to create the dictionary objects for the list of sections, for the options within a section, and for the default values. When `delimiters' is given, it will be used as the set of substrings that divide keys from values. When `comment_prefixes' is given, it will be used as the set of substrings that prefix comments in empty lines. Comments can be indented. When `inline_comment_prefixes' is given, it will be used as the set of substrings that prefix comments in non-empty lines. When `strict` is True, the parser won't allow for any section or option duplicates while reading from a single source (file, string or dictionary). Default is True. When `empty_lines_in_values' is False (default: True), each empty line marks the end of an option. Otherwise, internal empty lines of a multiline option are kept as part of the value. When `allow_no_value' is True (default: False), options without values are accepted; the value presented for these is None. sections() Return all the configuration section names, sans DEFAULT. has_section(section) Return whether the given section exists. has_option(section, option) Return whether the given option exists in the given section. options(section) Return list of configuration options for the named section. read(filenames, encoding=None) Read and parse the list of named configuration files, given by name. A single filename is also allowed. Non-existing files are ignored. Return list of successfully read files. read_file(f, filename=None) Read and parse one configuration file, given as a file object. The filename defaults to f.name; it is only used in error messages (if f has no `name' attribute, the string `<???>' is used). read_string(string) Read configuration from a given string. read_dict(dictionary) Read configuration from a dictionary. Keys are section names, values are dictionaries with keys and values that should be present in the section. If the used dictionary type preserves order, sections and their keys will be added in order. Values are automatically converted to strings. get(section, option, raw=False, vars=None, fallback=_UNSET) Return a string value for the named option. All % interpolations are expanded in the return values, based on the defaults passed into the constructor and the DEFAULT section. Additional substitutions may be provided using the `vars' argument, which must be a dictionary whose contents override any pre-existing defaults. If `option' is a key in `vars', the value from `vars' is used. getint(section, options, raw=False, vars=None, fallback=_UNSET) Like get(), but convert value to an integer. getfloat(section, options, raw=False, vars=None, fallback=_UNSET) Like get(), but convert value to a float. getboolean(section, options, raw=False, vars=None, fallback=_UNSET) Like get(), but convert value to a boolean (currently case insensitively defined as 0, false, no, off for False, and 1, true, yes, on for True). Returns False or True. items(section=_UNSET, raw=False, vars=None) If section is given, return a list of tuples with (name, value) for each option in the section. Otherwise, return a list of tuples with (section_name, section_proxy) for each section, including DEFAULTSECT. remove_section(section) Remove the given file section and all its options. remove_option(section, option) Remove the given option from the given section. set(section, option, value) Set the given option. write(fp, space_around_delimiters=True) Write the configuration state in .ini format. If `space_around_delimiters' is True (the default), delimiters between keys and values are surrounded by spaces. """
""" HTTP Exception -------------- This module processes Python exceptions that relate to HTTP exceptions by defining a set of exceptions, all subclasses of HTTPException. Each exception, in addition to being a Python exception that can be raised and caught, is also a WSGI application and ``webob.Response`` object. This module defines exceptions according to RFC 2068 [1]_ : codes with 100-300 are not really errors; 400's are client errors, and 500's are server errors. According to the WSGI specification [2]_ , the application can call ``start_response`` more then once only under two conditions: (a) the response has not yet been sent, or (b) if the second and subsequent invocations of ``start_response`` have a valid ``exc_info`` argument obtained from ``sys.exc_info()``. The WSGI specification then requires the server or gateway to handle the case where content has been sent and then an exception was encountered. Exception HTTPException HTTPOk * 200 - HTTPOk * 201 - HTTPCreated * 202 - HTTPAccepted * 203 - HTTPNonAuthoritativeInformation * 204 - HTTPNoContent * 205 - HTTPResetContent * 206 - HTTPPartialContent HTTPRedirection * 300 - HTTPMultipleChoices * 301 - HTTPMovedPermanently * 302 - HTTPFound * 303 - HTTPSeeOther * 304 - HTTPNotModified * 305 - HTTPUseProxy * 306 - Unused (not implemented, obviously) * 307 - HTTPTemporaryRedirect HTTPError HTTPClientError * 400 - HTTPBadRequest * 401 - HTTPUnauthorized * 402 - HTTPPaymentRequired * 403 - HTTPForbidden * 404 - HTTPNotFound * 405 - HTTPMethodNotAllowed * 406 - HTTPNotAcceptable * 407 - HTTPProxyAuthenticationRequired * 408 - HTTPRequestTimeout * 409 - HTTPConflict * 410 - HTTPGone * 411 - HTTPLengthRequired * 412 - HTTPPreconditionFailed * 413 - HTTPRequestEntityTooLarge * 414 - HTTPRequestURITooLong * 415 - HTTPUnsupportedMediaType * 416 - HTTPRequestRangeNotSatisfiable * 417 - HTTPExpectationFailed HTTPServerError * 500 - HTTPInternalServerError * 501 - HTTPNotImplemented * 502 - HTTPBadGateway * 503 - HTTPServiceUnavailable * 504 - HTTPGatewayTimeout * 505 - HTTPVersionNotSupported Subclass usage notes: --------------------- The HTTPException class is complicated by 4 factors: 1. The content given to the exception may either be plain-text or as html-text. 2. The template may want to have string-substitutions taken from the current ``environ`` or values from incoming headers. This is especially troublesome due to case sensitivity. 3. The final output may either be text/plain or text/html mime-type as requested by the client application. 4. Each exception has a default explanation, but those who raise exceptions may want to provide additional detail. Subclass attributes and call parameters are designed to provide an easier path through the complications. Attributes: ``code`` the HTTP status code for the exception ``title`` remainder of the status line (stuff after the code) ``explanation`` a plain-text explanation of the error message that is not subject to environment or header substitutions; it is accessible in the template via %(explanation)s ``detail`` a plain-text message customization that is not subject to environment or header substitutions; accessible in the template via %(detail)s ``body_template`` a content fragment (in HTML) used for environment and header substitution; the default template includes both the explanation and further detail provided in the message Parameters: ``detail`` a plain-text override of the default ``detail`` ``headers`` a list of (k,v) header pairs ``comment`` a plain-text additional information which is usually stripped/hidden for end-users ``body_template`` a string.Template object containing a content fragment in HTML that frames the explanation and further detail To override the template (which is HTML content) or the plain-text explanation, one must subclass the given exception; or customize it after it has been created. This particular breakdown of a message into explanation, detail and template allows both the creation of plain-text and html messages for various clients as well as error-free substitution of environment variables and headers. The subclasses of :class:`~_HTTPMove` (:class:`~HTTPMultipleChoices`, :class:`~HTTPMovedPermanently`, :class:`~HTTPFound`, :class:`~HTTPSeeOther`, :class:`~HTTPUseProxy` and :class:`~HTTPTemporaryRedirect`) are redirections that require a ``Location`` field. Reflecting this, these subclasses have two additional keyword arguments: ``location`` and ``add_slash``. Parameters: ``location`` to set the location immediately ``add_slash`` set to True to redirect to the same URL as the request, except with a ``/`` appended Relative URLs in the location will be resolved to absolute. References: .. [1] http://www.python.org/peps/pep-0333.html#error-handling .. [2] http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5 """
# # XML-RPC CLIENT LIBRARY # $Id$ # # an XML-RPC client interface for Python. # # the marshalling and response parser code can also be used to # implement XML-RPC servers. # # Notes: # this version is designed to work with Python 2.1 or newer. # # History: # 1999-01-14 fl Created # 1999-01-15 fl Changed dateTime to use localtime # 1999-01-16 fl Added Binary/base64 element, default to RPC2 service # 1999-01-19 fl Fixed array data element (from Skip Montanaro) # 1999-01-21 fl Fixed dateTime constructor, etc. # 1999-02-02 fl Added fault handling, handle empty sequences, etc. # 1999-02-10 fl Fixed problem with empty responses (from Skip Montanaro) # 1999-06-20 fl Speed improvements, pluggable parsers/transports (0.9.8) # 2000-11-28 fl Changed boolean to check the truth value of its argument # 2001-02-24 fl Added encoding/Unicode/SafeTransport patches # 2001-02-26 fl Added compare support to wrappers (0.9.9/1.0b1) # 2001-03-28 fl Make sure response tuple is a singleton # 2001-03-29 fl Don't require empty params element (from NAME 2001-06-10 fl Folded in _xmlrpclib accelerator support (1.0b2) # 2001-08-20 fl Base xmlrpclib.Error on built-in Exception (from NAME 2001-09-03 fl Allow Transport subclass to override getparser # 2001-09-10 fl Lazy import of urllib, cgi, xmllib (20x import speedup) # 2001-10-01 fl Remove containers from memo cache when done with them # 2001-10-01 fl Use faster escape method (80% dumps speedup) # 2001-10-02 fl More dumps microtuning # 2001-10-04 fl Make sure import expat gets a parser (from NAME 2001-10-10 sm Allow long ints to be passed as ints if they don't overflow # 2001-10-17 sm Test for int and long overflow (allows use on 64-bit systems) # 2001-11-12 fl Use repr() to marshal doubles (from NAME 2002-03-17 fl Avoid buffered read when possible (from NAME 2002-04-07 fl Added pythondoc comments # 2002-04-16 fl Added __str__ methods to datetime/binary wrappers # 2002-05-15 fl Added error constants (from NAME 2002-06-27 fl Merged with Python CVS version # 2002-10-22 fl Added basic authentication (based on code from NAME 2003-01-22 sm Add support for the bool type # 2003-02-27 gvr Remove apply calls # 2003-04-24 sm Use cStringIO if available # 2003-04-25 ak Add support for nil # 2003-06-15 gn Add support for time.struct_time # 2003-07-12 gp Correct marshalling of Faults # 2003-10-31 mvl Add multicall support # 2004-08-20 mvl Bump minimum supported Python version to 2.1 # # Copyright (c) 1999-2002 by Secret Labs AB. # Copyright (c) 1999-2002 by NAME EMAIL http://www.pythonware.com # # -------------------------------------------------------------------- # The XML-RPC client interface is # # Copyright (c) 1999-2002 by Secret Labs AB # Copyright (c) 1999-2002 by NAME By obtaining, using, and/or copying this software and/or its # associated documentation, you agree that you have read, understood, # and will comply with the following terms and conditions: # # Permission to use, copy, modify, and distribute this software and # its associated documentation for any purpose and without fee is # hereby granted, provided that the above copyright notice appears in # all copies, and that both that copyright notice and this permission # notice appear in supporting documentation, and that the name of # Secret Labs AB or the author not be used in advertising or publicity # pertaining to distribution of the software without specific, written # prior permission. # # SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD # TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT- # ABILITY AND FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE # OF THIS SOFTWARE. # -------------------------------------------------------------------- # # things to look into some day: # TODO: sort out True/False/boolean issues for Python 2.3
#!/usr/bin/env vpython # # [VPYTHON:BEGIN] # wheel: < # name: "infra/python/wheels/google-auth-py2_py3" # version: "version:1.2.1" # > # # wheel: < # name: "infra/python/wheels/pyasn1-py2_py3" # version: "version:0.4.5" # > # # wheel: < # name: "infra/python/wheels/pyasn1_modules-py2_py3" # version: "version:0.2.4" # > # # wheel: < # name: "infra/python/wheels/six" # version: "version:1.10.0" # > # # wheel: < # name: "infra/python/wheels/cachetools-py2_py3" # version: "version:2.0.1" # > # wheel: < # name: "infra/python/wheels/rsa-py2_py3" # version: "version:4.0" # > # # wheel: < # name: "infra/python/wheels/requests" # version: "version:2.13.0" # > # # wheel: < # name: "infra/python/wheels/google-api-python-client-py2_py3" # version: "version:1.6.2" # > # # wheel: < # name: "infra/python/wheels/httplib2-py2_py3" # version: "version:0.12.1" # > # # wheel: < # name: "infra/python/wheels/oauth2client-py2_py3" # version: "version:3.0.0" # > # # wheel: < # name: "infra/python/wheels/uritemplate-py2_py3" # version: "version:3.0.0" # > # # wheel: < # name: "infra/python/wheels/google-auth-oauthlib-py2_py3" # version: "version:0.3.0" # > # # wheel: < # name: "infra/python/wheels/requests-oauthlib-py2_py3" # version: "version:1.2.0" # > # # wheel: < # name: "infra/python/wheels/oauthlib-py2_py3" # version: "version:3.0.1" # > # # wheel: < # name: "infra/python/wheels/google-auth-httplib2-py2_py3" # version: "version:0.0.3" # > # [VPYTHON:END] # # Copyright 2019 The ANGLE Project Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. # # generate_deqp_stats.py: # Checks output of deqp testers and generates stats using the GDocs API # # prerequirements: # https://devsite.googleplex.com/sheets/api/quickstart/python # Follow the quickstart guide. # # usage: generate_deqp_stats.py [-h] [--auth_path [AUTH_PATH]] [--spreadsheet [SPREADSHEET]] # [--verbosity [VERBOSITY]] # # optional arguments: # -h, --help show this help message and exit # --auth_path [AUTH_PATH] # path to directory containing authorization data (credentials.json and # token.pickle). [default=<home>/.auth] # --spreadsheet [SPREADSHEET] # ID of the spreadsheet to write stats to. [default # ='1D6Yh7dAPP-aYLbX3HHQD8WubJV9XPuxvkKowmn2qhIw'] # --verbosity [VERBOSITY] # Verbosity of output. Valid options are [DEBUG, INFO, WARNING, ERROR]. # [default=INFO]
"""Configuration file parser. A configuration file consists of sections, lead by a "[section]" header, and followed by "name: value" entries, with continuations and such in the style of RFC 822. Intrinsic defaults can be specified by passing them into the ConfigParser constructor as a dictionary. class: ConfigParser -- responsible for parsing a list of configuration files, and managing the parsed database. methods: __init__(defaults=None, dict_type=_default_dict, allow_no_value=False, delimiters=('=', ':'), comment_prefixes=('#', ';'), inline_comment_prefixes=None, strict=True, empty_lines_in_values=True): Create the parser. When `defaults' is given, it is initialized into the dictionary or intrinsic defaults. The keys must be strings, the values must be appropriate for %()s string interpolation. When `dict_type' is given, it will be used to create the dictionary objects for the list of sections, for the options within a section, and for the default values. When `delimiters' is given, it will be used as the set of substrings that divide keys from values. When `comment_prefixes' is given, it will be used as the set of substrings that prefix comments in empty lines. Comments can be indented. When `inline_comment_prefixes' is given, it will be used as the set of substrings that prefix comments in non-empty lines. When `strict` is True, the parser won't allow for any section or option duplicates while reading from a single source (file, string or dictionary). Default is True. When `empty_lines_in_values' is False (default: True), each empty line marks the end of an option. Otherwise, internal empty lines of a multiline option are kept as part of the value. When `allow_no_value' is True (default: False), options without values are accepted; the value presented for these is None. sections() Return all the configuration section names, sans DEFAULT. has_section(section) Return whether the given section exists. has_option(section, option) Return whether the given option exists in the given section. options(section) Return list of configuration options for the named section. read(filenames, encoding=None) Read and parse the list of named configuration files, given by name. A single filename is also allowed. Non-existing files are ignored. Return list of successfully read files. read_file(f, filename=None) Read and parse one configuration file, given as a file object. The filename defaults to f.name; it is only used in error messages (if f has no `name' attribute, the string `<???>' is used). read_string(string) Read configuration from a given string. read_dict(dictionary) Read configuration from a dictionary. Keys are section names, values are dictionaries with keys and values that should be present in the section. If the used dictionary type preserves order, sections and their keys will be added in order. Values are automatically converted to strings. get(section, option, raw=False, vars=None, fallback=_UNSET) Return a string value for the named option. All % interpolations are expanded in the return values, based on the defaults passed into the constructor and the DEFAULT section. Additional substitutions may be provided using the `vars' argument, which must be a dictionary whose contents override any pre-existing defaults. If `option' is a key in `vars', the value from `vars' is used. getint(section, options, raw=False, vars=None, fallback=_UNSET) Like get(), but convert value to an integer. getfloat(section, options, raw=False, vars=None, fallback=_UNSET) Like get(), but convert value to a float. getboolean(section, options, raw=False, vars=None, fallback=_UNSET) Like get(), but convert value to a boolean (currently case insensitively defined as 0, false, no, off for False, and 1, true, yes, on for True). Returns False or True. items(section=_UNSET, raw=False, vars=None) If section is given, return a list of tuples with (name, value) for each option in the section. Otherwise, return a list of tuples with (section_name, section_proxy) for each section, including DEFAULTSECT. remove_section(section) Remove the given file section and all its options. remove_option(section, option) Remove the given option from the given section. set(section, option, value) Set the given option. write(fp, space_around_delimiters=True) Write the configuration state in .ini format. If `space_around_delimiters' is True (the default), delimiters between keys and values are surrounded by spaces. """
"""Configuration file parser. A configuration file consists of sections, lead by a "[section]" header, and followed by "name: value" entries, with continuations and such in the style of RFC 822. Intrinsic defaults can be specified by passing them into the ConfigParser constructor as a dictionary. class: ConfigParser -- responsible for parsing a list of configuration files, and managing the parsed database. methods: __init__(defaults=None, dict_type=_default_dict, allow_no_value=False, delimiters=('=', ':'), comment_prefixes=('#', ';'), inline_comment_prefixes=None, strict=True, empty_lines_in_values=True, default_section='DEFAULT', interpolation=<unset>, converters=<unset>): Create the parser. When `defaults' is given, it is initialized into the dictionary or intrinsic defaults. The keys must be strings, the values must be appropriate for %()s string interpolation. When `dict_type' is given, it will be used to create the dictionary objects for the list of sections, for the options within a section, and for the default values. When `delimiters' is given, it will be used as the set of substrings that divide keys from values. When `comment_prefixes' is given, it will be used as the set of substrings that prefix comments in empty lines. Comments can be indented. When `inline_comment_prefixes' is given, it will be used as the set of substrings that prefix comments in non-empty lines. When `strict` is True, the parser won't allow for any section or option duplicates while reading from a single source (file, string or dictionary). Default is True. When `empty_lines_in_values' is False (default: True), each empty line marks the end of an option. Otherwise, internal empty lines of a multiline option are kept as part of the value. When `allow_no_value' is True (default: False), options without values are accepted; the value presented for these is None. When `default_section' is given, the name of the special section is named accordingly. By default it is called ``"DEFAULT"`` but this can be customized to point to any other valid section name. Its current value can be retrieved using the ``parser_instance.default_section`` attribute and may be modified at runtime. When `interpolation` is given, it should be an Interpolation subclass instance. It will be used as the handler for option value pre-processing when using getters. RawConfigParser objects don't do any sort of interpolation, whereas ConfigParser uses an instance of BasicInterpolation. The library also provides a ``zc.buildbot`` inspired ExtendedInterpolation implementation. When `converters` is given, it should be a dictionary where each key represents the name of a type converter and each value is a callable implementing the conversion from string to the desired datatype. Every converter gets its corresponding get*() method on the parser object and section proxies. sections() Return all the configuration section names, sans DEFAULT. has_section(section) Return whether the given section exists. has_option(section, option) Return whether the given option exists in the given section. options(section) Return list of configuration options for the named section. read(filenames, encoding=None) Read and parse the iterable of named configuration files, given by name. A single filename is also allowed. Non-existing files are ignored. Return list of successfully read files. read_file(f, filename=None) Read and parse one configuration file, given as a file object. The filename defaults to f.name; it is only used in error messages (if f has no `name' attribute, the string `<???>' is used). read_string(string) Read configuration from a given string. read_dict(dictionary) Read configuration from a dictionary. Keys are section names, values are dictionaries with keys and values that should be present in the section. If the used dictionary type preserves order, sections and their keys will be added in order. Values are automatically converted to strings. get(section, option, raw=False, vars=None, fallback=_UNSET) Return a string value for the named option. All % interpolations are expanded in the return values, based on the defaults passed into the constructor and the DEFAULT section. Additional substitutions may be provided using the `vars' argument, which must be a dictionary whose contents override any pre-existing defaults. If `option' is a key in `vars', the value from `vars' is used. getint(section, options, raw=False, vars=None, fallback=_UNSET) Like get(), but convert value to an integer. getfloat(section, options, raw=False, vars=None, fallback=_UNSET) Like get(), but convert value to a float. getboolean(section, options, raw=False, vars=None, fallback=_UNSET) Like get(), but convert value to a boolean (currently case insensitively defined as 0, false, no, off for False, and 1, true, yes, on for True). Returns False or True. items(section=_UNSET, raw=False, vars=None) If section is given, return a list of tuples with (name, value) for each option in the section. Otherwise, return a list of tuples with (section_name, section_proxy) for each section, including DEFAULTSECT. remove_section(section) Remove the given file section and all its options. remove_option(section, option) Remove the given option from the given section. set(section, option, value) Set the given option. write(fp, space_around_delimiters=True) Write the configuration state in .ini format. If `space_around_delimiters' is True (the default), delimiters between keys and values are surrounded by spaces. """
# # ElementTree # $Id: ElementTree.py 2326 2005-03-17 07:45:21Z USERNAME $ # # light-weight XML support for Python 1.5.2 and later. # # history: # 2001-10-20 fl created (from various sources) # 2001-11-01 fl return root from parse method # 2002-02-16 fl sort attributes in lexical order # 2002-04-06 fl TreeBuilder refactoring, added PythonDoc markup # 2002-05-01 fl finished TreeBuilder refactoring # 2002-07-14 fl added basic namespace support to ElementTree.write # 2002-07-25 fl added QName attribute support # 2002-10-20 fl fixed encoding in write # 2002-11-24 fl changed default encoding to ascii; fixed attribute encoding # 2002-11-27 fl accept file objects or file names for parse/write # 2002-12-04 fl moved XMLTreeBuilder back to this module # 2003-01-11 fl fixed entity encoding glitch for us-ascii # 2003-02-13 fl added XML literal factory # 2003-02-21 fl added ProcessingInstruction/PI factory # 2003-05-11 fl added tostring/fromstring helpers # 2003-05-26 fl added ElementPath support # 2003-07-05 fl added makeelement factory method # 2003-07-28 fl added more well-known namespace prefixes # 2003-08-15 fl fixed typo in ElementTree.findtext (Thomas NAME 2003-09-04 fl fall back on emulator if ElementPath is not installed # 2003-10-31 fl markup updates # 2003-11-15 fl fixed nested namespace bug # 2004-03-28 fl added XMLID helper # 2004-06-02 fl added default support to findtext # 2004-06-08 fl fixed encoding of non-ascii element/attribute names # 2004-08-23 fl take advantage of post-2.1 expat features # 2005-02-01 fl added iterparse implementation # 2005-03-02 fl fixed iterparse support for pre-2.2 versions # # Copyright (c) 1999-2005 by NAME All rights reserved. # # EMAIL # http://www.pythonware.com # # -------------------------------------------------------------------- # The ElementTree toolkit is # # Copyright (c) 1999-2005 by NAME By obtaining, using, and/or copying this software and/or its # associated documentation, you agree that you have read, understood, # and will comply with the following terms and conditions: # # Permission to use, copy, modify, and distribute this software and # its associated documentation for any purpose and without fee is # hereby granted, provided that the above copyright notice appears in # all copies, and that both that copyright notice and this permission # notice appear in supporting documentation, and that the name of # Secret Labs AB or the author not be used in advertising or publicity # pertaining to distribution of the software without specific, written # prior permission. # # SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD # TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT- # ABILITY AND FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE # OF THIS SOFTWARE. # -------------------------------------------------------------------- # Licensed to PSF under a Contributor Agreement. # See http://www.python.org/2.4/license for licensing details.
""" Define a simple format for saving numpy arrays to disk with the full information about them. The ``.npy`` format is the standard binary file format in NumPy for persisting a *single* arbitrary NumPy array on disk. The format stores all of the shape and dtype information necessary to reconstruct the array correctly even on another machine with a different architecture. The format is designed to be as simple as possible while achieving its limited goals. The ``.npz`` format is the standard format for persisting *multiple* NumPy arrays on disk. A ``.npz`` file is a zip file containing multiple ``.npy`` files, one for each array. Capabilities ------------ - Can represent all NumPy arrays including nested record arrays and object arrays. - Represents the data in its native binary form. - Supports Fortran-contiguous arrays directly. - Stores all of the necessary information to reconstruct the array including shape and dtype on a machine of a different architecture. Both little-endian and big-endian arrays are supported, and a file with little-endian numbers will yield a little-endian array on any machine reading the file. The types are described in terms of their actual sizes. For example, if a machine with a 64-bit C "long int" writes out an array with "long ints", a reading machine with 32-bit C "long ints" will yield an array with 64-bit integers. - Is straightforward to reverse engineer. Datasets often live longer than the programs that created them. A competent developer should be able create a solution in his preferred programming language to read most ``.npy`` files that he has been given without much documentation. - Allows memory-mapping of the data. See `open_memmep`. - Can be read from a filelike stream object instead of an actual file. - Stores object arrays, i.e. arrays containing elements that are arbitrary Python objects. Files with object arrays are not to be mmapable, but can be read and written to disk. Limitations ----------- - Arbitrary subclasses of numpy.ndarray are not completely preserved. Subclasses will be accepted for writing, but only the array data will be written out. A regular numpy.ndarray object will be created upon reading the file. .. warning:: Due to limitations in the interpretation of structured dtypes, dtypes with fields with empty names will have the names replaced by 'f0', 'f1', etc. Such arrays will not round-trip through the format entirely accurately. The data is intact; only the field names will differ. We are working on a fix for this. This fix will not require a change in the file format. The arrays with such structures can still be saved and restored, and the correct dtype may be restored by using the ``loadedarray.view(correct_dtype)`` method. File extensions --------------- We recommend using the ``.npy`` and ``.npz`` extensions for files saved in this format. This is by no means a requirement; applications may wish to use these file formats but use an extension specific to the application. In the absence of an obvious alternative, however, we suggest using ``.npy`` and ``.npz``. Version numbering ----------------- The version numbering of these formats is independent of NumPy version numbering. If the format is upgraded, the code in `numpy.io` will still be able to read and write Version 1.0 files. Format Version 1.0 ------------------ The first 6 bytes are a magic string: exactly ``\\x93NUMPY``. The next 1 byte is an unsigned byte: the major version number of the file format, e.g. ``\\x01``. The next 1 byte is an unsigned byte: the minor version number of the file format, e.g. ``\\x00``. Note: the version of the file format is not tied to the version of the numpy package. The next 2 bytes form a little-endian unsigned short int: the length of the header data HEADER_LEN. The next HEADER_LEN bytes form the header data describing the array's format. It is an ASCII string which contains a Python literal expression of a dictionary. It is terminated by a newline (``\\n``) and padded with spaces (``\\x20``) to make the total length of ``magic string + 4 + HEADER_LEN`` be evenly divisible by 16 for alignment purposes. The dictionary contains three keys: "descr" : dtype.descr An object that can be passed as an argument to the `numpy.dtype` constructor to create the array's dtype. "fortran_order" : bool Whether the array data is Fortran-contiguous or not. Since Fortran-contiguous arrays are a common form of non-C-contiguity, we allow them to be written directly to disk for efficiency. "shape" : tuple of int The shape of the array. For repeatability and readability, the dictionary keys are sorted in alphabetic order. This is for convenience only. A writer SHOULD implement this if possible. A reader MUST NOT depend on this. Following the header comes the array data. If the dtype contains Python objects (i.e. ``dtype.hasobject is True``), then the data is a Python pickle of the array. Otherwise the data is the contiguous (either C- or Fortran-, depending on ``fortran_order``) bytes of the array. Consumers can figure out the number of bytes by multiplying the number of elements given by the shape (noting that ``shape=()`` means there is 1 element) by ``dtype.itemsize``. Notes ----- The ``.npy`` format, including reasons for creating it and a comparison of alternatives, is described fully in the "npy-format" NEP. """
# -*- encoding: utf-8 -*- ############################################################################## # # Copyright (c) 2009 Veritos - NAME - www.veritos.nl # # WARNING: This program as such is intended to be used by professional # programmers who take the whole responsability of assessing all potential # consequences resulting from its eventual inadequacies and bugs. # End users who are looking for a ready-to-use solution with commercial # garantees and support are strongly adviced to contract a Free Software # Service Company like Veritos. # # This program is Free Software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA # ############################################################################## # # Deze module werkt in OpenERP 5.0.0 (en waarschijnlijk hoger). # Deze module werkt niet in OpenERP versie 4 en lager. # # Status 1.0 - getest op OpenERP 5.0.3 # # Versie IP_ADDRESS # account.account.type # Basis gelegd voor alle account type # # account.account.template # Basis gelegd met alle benodigde grootboekrekeningen welke via een menu- # structuur gelinkt zijn aan rubrieken 1 t/m 9. # De grootboekrekeningen gelinkt aan de account.account.type # Deze links moeten nog eens goed nagelopen worden. # # account.chart.template # Basis gelegd voor het koppelen van rekeningen aan debiteuren, crediteuren, # bank, inkoop en verkoop boeken en de BTW configuratie. # # Versie IP_ADDRESS # account.tax.code.template # Basis gelegd voor de BTW configuratie (structuur) # Heb als basis het BTW aangifte formulier gebruikt. Of dit werkt? # # account.tax.template # De BTW rekeningen aangemaakt en deze gekoppeld aan de betreffende # grootboekrekeningen # # Versie IP_ADDRESS # Opschonen van de code en verwijderen van niet gebruikte componenten. # Versie IP_ADDRESS # Aanpassen a_expense van 3000 -> 7000 # record id='btw_code_5b' op negatieve waarde gezet # Versie IP_ADDRESS # BTW rekeningen hebben typeaanduiding gekregen t.b.v. purchase of sale # Versie IP_ADDRESS # Opschonen van module. # Versie IP_ADDRESS # Opschonen van module. # Versie IP_ADDRESS # Foutje in l10n_nl_wizard.xml gecorrigeerd waardoor de module niet volledig installeerde. # Versie IP_ADDRESS # Account Receivable en Payable goed gedefinieerd. # Versie IP_ADDRESS # Alle user_type_xxx velden goed gedefinieerd. # Specifieke bouw en garage gerelateerde grootboeken verwijderd om een standaard module te creeeren. # Deze module kan dan als basis worden gebruikt voor specifieke doelgroep modules te creeeren. # Versie IP_ADDRESS # Correctie van rekening 7010 (stond dubbel met 7014 waardoor installatie verkeerd ging) # versie IP_ADDRESS # Correctie op diverse rekening types van user_type_asset -> user_type_liability en user_type_equity # versie IP_ADDRESS # Kleine correctie op BTW te vorderen hoog, id was hetzelfde voor beide, waardoor hoog werd overschreven door # overig. Verduidelijking van omschrijvingen in belastingcodes t.b.v. aangifte overzicht. # versie IP_ADDRESS # BTW omschrijvingen aangepast, zodat rapporten er beter uitzien. 2a en 5b e.d. verwijderd en enkele omschrijvingen toegevoegd. # versie IP_ADDRESS - Switch to English # Added properties_stock_xxx accounts for correct stock valuation, changed 7000-accounts from type cash to type expense # Changed naming of 7020 and 7030 to Kostprijs omzet xxxx
""" This module contains dsolve() and different helper functions that it uses. dsolve() solves ordinary differential equations. See the docstring on the various functions for their uses. Note that partial differential equations support is in pde.py. Note that ode_hint() functions have docstrings describing their various methods, but they are intended for internal use. Use dsolve(ode, func, hint=hint) to solve an ode using a specific hint. See also the docstring on dsolve(). **Functions in this module** These are the user functions in this module: - dsolve() - Solves ODEs. - classify_ode() - Classifies ODEs into possible hints for dsolve(). - checkodesol() - Checks if an equation is the solution to an ODE. - ode_order() - Returns the order (degree) of an ODE. - homogeneous_order() - Returns the homogeneous order of an expression. These are the non-solver helper functions that are for internal use. The user should use the various options to dsolve() to obtain the functionality provided by these functions: - odesimp() - Does all forms of ODE simplification. - ode_sol_simplicity() - A key function for comparing solutions by simplicity. - constantsimp() - Simplifies arbitrary constants. - constant_renumber() - Renumber arbitrary constants - _handle_Integral() - Evaluate unevaluated Integrals. See also the docstrings of these functions. **Solving methods currently implemented** The following methods are implemented for solving ordinary differential equations. See the docstrings of the various ode_hint() functions for more information on each (run help(ode)): - 1st order separable differential equations - 1st order differential equations whose coefficients or dx and dy are functions homogeneous of the same order. - 1st order exact differential equations. - 1st order linear differential equations - 1st order Bernoulli differential equations. - 2nd order Liouville differential equations. - nth order linear homogeneous differential equation with constant coefficients. - nth order linear inhomogeneous differential equation with constant coefficients using the method of undetermined coefficients. - nth order linear inhomogeneous differential equation with constant coefficients using the method of variation of parameters. **Philosophy behind this module** This module is designed to make it easy to add new ODE solving methods without having to mess with the solving code for other methods. The idea is that there is a classify_ode() function, which takes in an ODE and tells you what hints, if any, will solve the ODE. It does this without attempting to solve the ODE, so it is fast. Each solving method is a hint, and it has its own function, named ode_hint. That function takes in the ODE and any match expression gathered by classify_ode and returns a solved result. If this result has any integrals in it, the ode_hint function will return an unevaluated Integral class. dsolve(), which is the user wrapper function around all of this, will then call odesimp() on the result, which, among other things, will attempt to solve the equation for the dependent variable (the function we are solving for), simplify the arbitrary constants in the expression, and evaluate any integrals, if the hint allows it. **How to add new solution methods** If you have an ODE that you want dsolve() to be able to solve, try to avoid adding special case code here. Instead, try finding a general method that will solve your ODE, as well as others. This way, the ode module will become more robust, and unhindered by special case hacks. WolphramAlpha and Maple's DETools[odeadvisor] function are two resources you can use to classify a specific ODE. It is also better for a method to work with an nth order ODE instead of only with specific orders, if possible. To add a new method, there are a few things that you need to do. First, you need a hint name for your method. Try to name your hint so that it is unambiguous with all other methods, including ones that may not be implemented yet. If your method uses integrals, also include a "hint_Integral" hint. If there is more than one way to solve ODEs with your method, include a hint for each one, as well as a "hint_best" hint. Your ode_hint_best() function should choose the best using min with ode_sol_simplicity as the key argument. See ode_1st_homogeneous_coeff_best(), for example. The function that uses your method will be called ode_hint(), so the hint must only use characters that are allowed in a Python function name (alphanumeric characters and the underscore '_' character). Include a function for every hint, except for "_Integral" hints (dsolve() takes care of those automatically). Hint names should be all lowercase, unless a word is commonly capitalized (such as Integral or Bernoulli). If you have a hint that you do not want to run with "all_Integral" that doesn't have an "_Integral" counterpart (such as a best hint that would defeat the purpose of "all_Integral"), you will need to remove it manually in the dsolve() code. See also the classify_ode() docstring for guidelines on writing a hint name. Determine *in general* how the solutions returned by your method compare with other methods that can potentially solve the same ODEs. Then, put your hints in the allhints tuple in the order that they should be called. The ordering of this tuple determines which hints are default. Note that exceptions are ok, because it is easy for the user to choose individual hints with dsolve(). In general, "_Integral" variants should go at the end of the list, and "_best" variants should go before the various hints they apply to. For example, the "undetermined_coefficients" hint comes before the "variation_of_parameters" hint because, even though variation of parameters is more general than undetermined coefficients, undetermined coefficients generally returns cleaner results for the ODEs that it can solve than variation of parameters does, and it does not require integration, so it is much faster. Next, you need to have a match expression or a function that matches the type of the ODE, which you should put in classify_ode() (if the match function is more than just a few lines, like _undetermined_coefficients_match(), it should go outside of classify_ode()). It should match the ODE without solving for it as much as possible, so that classify_ode() remains fast and is not hindered by bugs in solving code. Be sure to consider corner cases. For example, if your solution method involves dividing by something, make sure you exclude the case where that division will be 0. In most cases, the matching of the ODE will also give you the various parts that you need to solve it. You should put that in a dictionary (.match() will do this for you), and add that as matching_hints['hint'] = matchdict in the relevant part of classify_ode. classify_ode will then send this to dsolve(), which will send it to your function as the match argument. Your function should be named ode_hint(eq, func, order, match). If you need to send more information, put it in the match dictionary. For example, if you had to substitute in a dummy variable in classify_ode to match the ODE, you will need to pass it to your function using the match dict to access it. You can access the independent variable using func.args[0], and the dependent variable (the function you are trying to solve for) as func.func. If, while trying to solve the ODE, you find that you cannot, raise NotImplementedError. dsolve() will catch this error with the "all" meta-hint, rather than causing the whole routine to fail. Add a docstring to your function that describes the method employed. Like with anything else in SymPy, you will need to add a doctest to the docstring, in addition to real tests in test_ode.py. Try to maintain consistency with the other hint functions' docstrings. Add your method to the list at the top of this docstring. Also, add your method to ode.txt in the docs/src directory, so that the Sphinx docs will pull its docstring into the main SymPy documentation. Be sure to make the Sphinx documentation by running "make html" from within the doc directory to verify that the docstring formats correctly. If your solution method involves integrating, use C.Integral() instead of integrate(). This allows the user to bypass hard/slow integration by using the "_Integral" variant of your hint. In most cases, calling .doit() will integrate your solution. If this is not the case, you will need to write special code in _handle_Integral(). Arbitrary constants should be symbols named C1, C2, and so on. All solution methods should return an equality instance. If you need an arbitrary number of arbitrary constants, you can use constants = numbered_symbols(prefix='C', function=Symbol, start=1). If it is possible to solve for the dependent function in a general way, do so. Otherwise, do as best as you can, but do not call solve in your ode_hint() function. odesimp() will attempt to solve the solution for you, so you do not need to do that. Lastly, if your ODE has a common simplification that can be applied to your solutions, you can add a special case in odesimp() for it. For example, solutions returned from the "1st_homogeneous_coeff" hints often have many log() terms, so odesimp() calls logcombine() on them (it also helps to write the arbitrary constant as log(C1) instead of C1 in this case). Also consider common ways that you can rearrange your solution to have constantsimp() take better advantage of it. It is better to put simplification in odesimp() than in your method, because it can then be turned off with the simplify flag in dsolve(). If you have any extraneous simplification in your function, be sure to only run it using "if match.get('simplify', True):", especially if it can be slow or if it can reduce the domain of the solution. Finally, as with every contribution to SymPy, your method will need to be tested. Add a test for each method in test_ode.py. Follow the conventions there, i.e., test the solver using dsolve(eq, f(x), hint=your_hint), and also test the solution using checkodesol (you can put these in a separate tests and skip/XFAIL if it runs too slow/doesn't work). Be sure to call your hint specifically in dsolve, that way the test won't be broken simply by the introduction of another matching hint. If your method works for higher order (>1) ODEs, you will need to run sol = constant_renumber(sol, 'C', 1, order), for each solution, where order is the order of the ODE. This is because constant_renumber renumbers the arbitrary constants by printing order, which is platform dependent. Try to test every corner case of your solver, including a range of orders if it is a nth order solver, but if your solver is slow, auch as if it involves hard integration, try to keep the test run time down. Feel free to refactor existing hints to avoid duplicating code or creating inconsistencies. If you can show that your method exactly duplicates an existing method, including in the simplicity and speed of obtaining the solutions, then you can remove the old, less general method. The existing code is tested extensively in test_ode.py, so if anything is broken, one of those tests will surely fail. """
""" The event module provides a simple system for properties and events, to let different components of an application react to each-other and to user input. In short: * The :class:`HasEvents <flexx.event.HasEvents>` class provides objects that have properties and can emit events. * There are three decorators to create :func:`properties <flexx.event.prop>`, :func:`readonlies <flexx.event.readonly>` and :func:`emitters <flexx.event.emitter>`. * There is a decorator to :func:`connect <flexx.event.connect>` a method to an event. Event ----- An event is something that has occurred at a certain moment in time, such as the mouse being pressed down or a property changing its value. In this framework events are represented with dictionary objects that provide information about the event (such as what button was pressed, or the old and new value of a property). A custom :class:`Dict <flexx.event.Dict>` class is used that inherits from ``dict`` but allows attribute access, e.g. ``ev.button`` as an alternative to ``ev['button']``. The HasEvents class ------------------- The :class:`HasEvents <flexx.event.HasEvents>` class provides a base class for objects that have properties and/or emit events. E.g. a ``flexx.ui.Widget`` inherits from ``flexx.app.Model``, which inherits from ``flexx.event.HasEvents``. Events are emitted using the :func:`emit() <flexx.event.HasEvents.emit>` method, which accepts a name for the type of the event, and optionally a dict, e.g. ``emitter.emit('mouse_down', dict(button=1, x=103, y=211))``. The HasEvents object will add two attributes to the event: ``source``, a reference to the HasEvents object itself, and ``type``, a string indicating the type of the event. As a user, you generally do not need to emit events explicitly; events are automatically emitted, e.g. when setting a property. Handler ------- A handler is an object that can handle events. Handlers can be created using the :func:`connect <flexx.event.connect>` decorator: .. code-block:: python from flexx import event class MyObject(event.HasEvents): @event.connect('foo') def handle_foo(self, *events): print(events) ob = MyObject() ob.emit('foo', dict(value=42)) # will invoke handle_foo() This example demonstrates a few concepts. Firstly, the handler is connected via a *connection-string* that specifies the type of the event; in this case the handler is connected to the event-type "foo" of the object. This connection-string can also be a path, e.g. "sub.subsub.event_type". This allows for some powerful mechanics, as discussed in the section on dynamism. One can also see that the handler function accepts ``*events`` argument. This is because handlers can be passed zero or more events. If a handler is called manually (e.g. ``ob.handle_foo()``) it will have zero events. When called by the event system, it will have at least 1 event. When e.g. a property is set twice, the handler function is called just once, with multiple events, in the next event loop iteration. It is up to the programmer to determine whether only one action is required, or whether all events need processing. In the latter case, just use ``for ev in events: ...``. In most cases, you will connect to events that are known beforehand, like those they correspond to properties, readonlies and emitters. If you connect to an event that is not known (as in the example above) it might be a typo and Flexx will display a warning. Use `'!foo'` as a connection string (i.e. prepend an exclamation mark) to suppress such warnings. Another useful feature of the event system is that a handler can connect to multiple events at once: .. code-block:: python class MyObject(event.HasEvents): @event.connect('foo', 'bar') def handle_foo_and_bar(self, *events): print(events) To create a handler from a normal function, use the :func:`HasEvents.connect() <flexx.event.HasEvents.connect>` method: .. code-block:: python h = event.HasEvents() # Using a decorator @h.connect('foo', 'bar') def handle_func1(self, *events): print(events) # Explicit notation def handle_func2(self, *events): print(events) h.connect(handle_func2, 'foo', 'bar') Event emitters -------------- Apart from using :func:`emit() <flexx.event.HasEvents.emit>` there are certain attributes of ``HasEvents`` instances that generate events. Properties ========== Settable properties can be created easiliy using the :func:`prop <flexx.event.prop>` decorator: .. code-block:: python class MyObject(event.HasEvents): @event.prop def foo(self, v=0): ''' This is a float indicating bla bla ... ''' return float(v) The function that is decorated is essentially the setter function, and should have one argument (the new value for the property), which can have a default value (representing the initial value). The function body is used to validate and normalize the provided input. In this case the input is simply cast to a float. The docstring of the function will be the docstring of the property (e.g. for Sphynx docs). An alternative initial value for a property can be provided upon instantiation: .. code-block:: python m = MyObject(foo=3) Readonly ======== Readonly properties are created with the :func:`readonly <flexx.event.readonly>` decorator. The value of a readonly property can be set internally using the :func:`_set_prop() <flexx.event.HasEvents._set_prop>` method:. .. code-block:: python class MyObject(event.HasEvents): @event.readonly def foo(self, v=0): ''' This is a float indicating bla. ''' return float(v) def _somewhere(self): self._set_prop('foo', 42) Emitter ======= Emitter attributes make it easy to generate events, and function as a placeholder to document events on a class. They are created with the :func:`emitter <flexx.event.emitter>` decorator. .. code-block:: python class MyObject(event.HasEvents): @event.emitter def mouse_down(self, js_event): ''' Event emitted when the mouse is pressed down. ''' return dict(button=js_event.button) Emitters can have any number of arguments and should return a dictionary, which will get emitted as an event, with the event type matching the name of the emitter. Labels ------ Labels are a feature that makes it possible to infuence the order by which event handlers are called, and provide a means to disconnect specific (groups of) handlers. The label is part of the connection string: 'foo.bar:label'. .. code-block:: python class MyObject(event.HasEvents): @event.connect('foo') def given_foo_handler(*events): ... @event.connect('foo:aa') def my_foo_handler(*events): # This one is called first: 'aa' < 'given_f...' ... When an event is emitted, the event is added to the pending events of the handlers in the order of a key, which is the label if present, and otherwise the name of the handler. Note that this does not guarantee the order in case a handler has multiple connections: a handler can be scheduled to handle its events due to another event, and a handler always handles all its pending events at once. The label can also be used in the :func:`disconnect() <flexx.event.HasEvents.disconnect>` method: .. code-block:: python @h.connect('foo:mylabel') def handle_foo(*events): ... ... h.disconnect('foo:mylabel') # don't need reference to handle_foo Dynamism -------- Dynamism is a concept that allows one to connect to events for which the source can change. For the following example, assume that ``Node`` is a ``HasEvents`` subclass that has properties ``parent`` and ``children``. .. code-block:: python main = Node() main.parent = Node() main.children = Node(), Node() @main.connect('parent.foo') def parent_foo_handler(*events): ... @main.connect('children*.foo') def children_foo_handler(*events): ... The ``parent_foo_handler`` gets invoked when the "foo" event gets emitted on the parent of main. Similarly, the ``children_foo_handler`` gets invoked when any of the children emits its "foo" event. Note that in some cases you might also want to connect to changes of the ``parent`` or ``children`` property itself. The event system automatically reconnects handlers when necessary. This concept makes it very easy to connect to the right events without the need for a lot of boilerplate code. Note that the above example would also work if ``parent`` would be a regular attribute instead of a property, but the handler would not be automatically reconnected when it changed. Patterns -------- This event system is quite flexible and designed to cover the needs of a variety of event/messaging mechanisms. This section discusses how this system relates to some common patterns, and how these can be implemented. Observer pattern ================ The idea of the observer pattern is that observers keep track (the state of) of an object, and that object is agnostic about what it's tracked by. For example, in a music player, instead of writing code to update the window-title inside the function that starts a song, there would be a concept of a "current song", and the window would listen for changes to the current song to update the title when it changes. In ``flexx.event``, a ``HasEvents`` object keeps track of its observers (handlers) and notifies them when there are changes. In our music player example, there would be a property "current_song", and a handler to take action when it changes. As is common in the observer pattern, the handlers keep track of the handlers that they observe. Therefore both handlers and ``HasEvents`` objects have a ``dispose()`` method for cleaning up. Signals and slots ================= The Qt GUI toolkit makes use of a mechanism called "signals and slots" as an easy way to connect different components of an application. In ``flexx.event`` signals translate to readonly properties, and slots to the handlers that connect to them. Overloadable event handlers =========================== In Qt, the "event system" consists of methods that handles an event, which can be overloaded in subclasses to handle an event differently. In ``flexx.event``, handlers can similarly be re-implemented in subclasses, and these can call the original handler using ``super()`` if needed. Publish-subscribe pattern ========================== In pub-sub, publishers generate messages identified by a 'topic', and subscribers can subscribe to such topics. There can be zero or more publishers and zero or more subscribers to any topic. In ``flexx.event`` a `HasEvents` object can play the role of a broker. Publishers can simply emit events. The event type represents the message topic. Subscribers are represented by handlers. """
#!/usr/bin/env python # coding: utf-8 # --- # syncID: e6ccf19a4b454ca594388eeaa88ebe12 # title: "Calculate Vegetation Biomass from LiDAR Data in Python" # description: "Learn to calculate the biomass of standing vegetation using a canopy height model data product." # dateCreated: 2017-06-21 # authors: NAME contributors: NAME estimatedTime: 1 hour # packagesLibraries: numpy, gdal, matplotlib, matplotlib.pyplot, os # topics: lidar,remote-sensing # languagesTool: python # dataProduct: DP1.10098.001, DP3.30015.001, # code1: https://raw.githubusercontent.com/NEONScience/NEON-Data-Skills/main/tutorials/Python/Lidar/lidar-biomass/calc-biomass_py/calc-biomass_py.ipynb # tutorialSeries: intro-lidar-py-series # urlTitle: calc-biomass-py # --- # <div id="ds-objectives" markdown="1"> # # In this tutorial, we will calculate the biomass for a section of the SJER site. We # will be using the Canopy Height Model discrete LiDAR data product as well as NEON # field data on vegetation data. This tutorial will calculate Biomass for individual # trees in the forest. # # ### Objectives # After completing this tutorial, you will be able to: # # * Learn how to apply a guassian smoothing fernal for high-frequency spatial filtering # * Apply a watershed segmentation algorithm for delineating tree crowns # * Calculate biomass predictor variables from a CHM # * Setup training data for Biomass predictions # * Apply a Random Forest machine learning approach to calculate biomass # # # ### Install Python Packages # # * **numpy** # * **gdal** # * **matplotlib** # * **matplotlib.pyplot** # * **os** # # # ### Download Data # # If you have already downloaded the data set for the Data Institute, you have the # data for this tutorial within the SJER directory. If you would like to just # download the data for this tutorial use the following link. # # <a href="https://neondata.sharefile.com/d-s58db39240bf49ac8" class="link--button link--arrow"> # Download the Biomass Calculation teaching data subset</a> # # </div> # In this tutorial, we will calculate the biomass for a section of the SJER site. We # will be using the Canopy Height Model discrete LiDAR data product as well as NEON # field data on vegetation data. This tutorial will calculate Biomass for individual # trees in the forest. # # The calculation of biomass consists of four primary steps: # # 1. Delineating individual tree crowns # 2. Calculating predictor variables for all individuals # 3. Collecting training data # 4. Applying a regression model to estiamte biomass from predictors # # In this tutorial we will use a watershed segmentation algorithm for delineating # tree crowns (step 1) and and a Random Forest (RF) machine learning algorithm for # relating the predictor variables to biomass (part 4). The predictor variables were # selected following suggestions by Gleason et al. (2012) and biomass estimates were # determined from DBH (diamter at breast height) measurements following relationships # given in Jenkins et al. (2003). # # ## Get Started # # First, we need to specify the directory where we will find and save the data needed for this tutorial. You will need to change this line to suit your local machine. I have decided to save my data in the following directory: # In[1]:
""" Basic functions used by several sub-packages and useful to have in the main name-space. Type Handling ------------- ================ =================== iscomplexobj Test for complex object, scalar result isrealobj Test for real object, scalar result iscomplex Test for complex elements, array result isreal Test for real elements, array result imag Imaginary part real Real part real_if_close Turns complex number with tiny imaginary part to real isneginf Tests for negative infinity, array result isposinf Tests for positive infinity, array result isnan Tests for nans, array result isinf Tests for infinity, array result isfinite Tests for finite numbers, array result isscalar True if argument is a scalar nan_to_num Replaces NaN's with 0 and infinities with large numbers cast Dictionary of functions to force cast to each type common_type Determine the minimum common type code for a group of arrays mintypecode Return minimal allowed common typecode. ================ =================== Index Tricks ------------ ================ =================== mgrid Method which allows easy construction of N-d 'mesh-grids' ``r_`` Append and construct arrays: turns slice objects into ranges and concatenates them, for 2d arrays appends rows. index_exp Konrad Hinsen's index_expression class instance which can be useful for building complicated slicing syntax. ================ =================== Useful Functions ---------------- ================ =================== select Extension of where to multiple conditions and choices extract Extract 1d array from flattened array according to mask insert Insert 1d array of values into Nd array according to mask linspace Evenly spaced samples in linear space logspace Evenly spaced samples in logarithmic space fix Round x to nearest integer towards zero mod Modulo mod(x,y) = x % y except keeps sign of y amax Array maximum along axis amin Array minimum along axis ptp Array max-min along axis cumsum Cumulative sum along axis prod Product of elements along axis cumprod Cumluative product along axis diff Discrete differences along axis angle Returns angle of complex argument unwrap Unwrap phase along given axis (1-d algorithm) sort_complex Sort a complex-array (based on real, then imaginary) trim_zeros Trim the leading and trailing zeros from 1D array. vectorize A class that wraps a Python function taking scalar arguments into a generalized function which can handle arrays of arguments using the broadcast rules of numerix Python. ================ =================== Shape Manipulation ------------------ ================ =================== squeeze Return a with length-one dimensions removed. atleast_1d Force arrays to be > 1D atleast_2d Force arrays to be > 2D atleast_3d Force arrays to be > 3D vstack Stack arrays vertically (row on row) hstack Stack arrays horizontally (column on column) column_stack Stack 1D arrays as columns into 2D array dstack Stack arrays depthwise (along third dimension) split Divide array into a list of sub-arrays hsplit Split into columns vsplit Split into rows dsplit Split along third dimension ================ =================== Matrix (2D Array) Manipulations ------------------------------- ================ =================== fliplr 2D array with columns flipped flipud 2D array with rows flipped rot90 Rotate a 2D array a multiple of 90 degrees eye Return a 2D array with ones down a given diagonal diag Construct a 2D array from a vector, or return a given diagonal from a 2D array. mat Construct a Matrix bmat Build a Matrix from blocks ================ =================== Polynomials ----------- ================ =================== poly1d A one-dimensional polynomial class poly Return polynomial coefficients from roots roots Find roots of polynomial given coefficients polyint Integrate polynomial polyder Differentiate polynomial polyadd Add polynomials polysub Substract polynomials polymul Multiply polynomials polydiv Divide polynomials polyval Evaluate polynomial at given argument ================ =================== Import Tricks ------------- ================ =================== ppimport Postpone module import until trying to use it ppimport_attr Postpone module import until trying to use its attribute ppresolve Import postponed module and return it. ================ =================== Machine Arithmetics ------------------- ================ =================== machar_single Single precision floating point arithmetic parameters machar_double Double precision floating point arithmetic parameters ================ =================== Threading Tricks ---------------- ================ =================== ParallelExec Execute commands in parallel thread. ================ =================== 1D Array Set Operations ----------------------- Set operations for 1D numeric arrays based on sort() function. ================ =================== ediff1d Array difference (auxiliary function). unique Unique elements of an array. intersect1d Intersection of 1D arrays with unique elements. setxor1d Set exclusive-or of 1D arrays with unique elements. in1d Test whether elements in a 1D array are also present in another array. union1d Union of 1D arrays with unique elements. setdiff1d Set difference of 1D arrays with unique elements. ================ =================== """
""" Numerical python functions written for compatability with matlab(TM) commands with the same names. Matlab(TM) compatible functions ------------------------------- :func:`cohere` Coherence (normalized cross spectral density) :func:`csd` Cross spectral density uing Welch's average periodogram :func:`detrend` Remove the mean or best fit line from an array :func:`find` Return the indices where some condition is true; numpy.nonzero is similar but more general. :func:`griddata` interpolate irregularly distributed data to a regular grid. :func:`prctile` find the percentiles of a sequence :func:`prepca` Principal Component Analysis :func:`psd` Power spectral density uing Welch's average periodogram :func:`rk4` A 4th order runge kutta integrator for 1D or ND systems :func:`specgram` Spectrogram (power spectral density over segments of time) Miscellaneous functions ------------------------- Functions that don't exist in matlab(TM), but are useful anyway: :meth:`cohere_pairs` Coherence over all pairs. This is not a matlab function, but we compute coherence a lot in my lab, and we compute it for a lot of pairs. This function is optimized to do this efficiently by caching the direct FFTs. :meth:`rk4` A 4th order Runge-Kutta ODE integrator in case you ever find yourself stranded without scipy (and the far superior scipy.integrate tools) record array helper functions ------------------------------- A collection of helper methods for numpyrecord arrays .. _htmlonly:: See :ref:`misc-examples-index` :meth:`rec2txt` pretty print a record array :meth:`rec2csv` store record array in CSV file :meth:`csv2rec` import record array from CSV file with type inspection :meth:`rec_append_fields` adds field(s)/array(s) to record array :meth:`rec_drop_fields` drop fields from record array :meth:`rec_join` join two record arrays on sequence of fields :meth:`rec_groupby` summarize data by groups (similar to SQL GROUP BY) :meth:`rec_summarize` helper code to filter rec array fields into new fields For the rec viewer functions(e rec2csv), there are a bunch of Format objects you can pass into the functions that will do things like color negative values red, set percent formatting and scaling, etc. Example usage:: r = csv2rec('somefile.csv', checkrows=0) formatd = dict( weight = FormatFloat(2), change = FormatPercent(2), cost = FormatThousands(2), ) rec2excel(r, 'test.xls', formatd=formatd) rec2csv(r, 'test.csv', formatd=formatd) scroll = rec2gtk(r, formatd=formatd) win = gtk.Window() win.set_size_request(600,800) win.add(scroll) win.show_all() gtk.main() Deprecated functions --------------------- The following are deprecated; please import directly from numpy (with care--function signatures may differ): :meth:`conv` convolution (numpy.convolve) :meth:`corrcoef` The matrix of correlation coefficients :meth:`hist` Histogram (numpy.histogram) :meth:`linspace` Linear spaced array from min to max :meth:`load` load ASCII file - use numpy.loadtxt :meth:`meshgrid` Make a 2D grid from 2 1 arrays (numpy.meshgrid) :meth:`polyfit` least squares best polynomial fit of x to y (numpy.polyfit) :meth:`polyval` evaluate a vector for a vector of polynomial coeffs (numpy.polyval) :meth:`save` save ASCII file - use numpy.savetxt :meth:`trapz` trapeziodal integration (trapz(x,y) -> numpy.trapz(y,x)) :meth:`vander` the Vandermonde matrix (numpy.vander) """
"""Configuration file parser. A configuration file consists of sections, lead by a "[section]" header, and followed by "name: value" entries, with continuations and such in the style of RFC 822. Intrinsic defaults can be specified by passing them into the ConfigParser constructor as a dictionary. class: ConfigParser -- responsible for parsing a list of configuration files, and managing the parsed database. methods: __init__(defaults=None, dict_type=_default_dict, allow_no_value=False, delimiters=('=', ':'), comment_prefixes=('#', ';'), inline_comment_prefixes=None, strict=True, empty_lines_in_values=True): Create the parser. When `defaults' is given, it is initialized into the dictionary or intrinsic defaults. The keys must be strings, the values must be appropriate for %()s string interpolation. When `dict_type' is given, it will be used to create the dictionary objects for the list of sections, for the options within a section, and for the default values. When `delimiters' is given, it will be used as the set of substrings that divide keys from values. When `comment_prefixes' is given, it will be used as the set of substrings that prefix comments in empty lines. Comments can be indented. When `inline_comment_prefixes' is given, it will be used as the set of substrings that prefix comments in non-empty lines. When `strict` is True, the parser won't allow for any section or option duplicates while reading from a single source (file, string or dictionary). Default is True. When `empty_lines_in_values' is False (default: True), each empty line marks the end of an option. Otherwise, internal empty lines of a multiline option are kept as part of the value. When `allow_no_value' is True (default: False), options without values are accepted; the value presented for these is None. sections() Return all the configuration section names, sans DEFAULT. has_section(section) Return whether the given section exists. has_option(section, option) Return whether the given option exists in the given section. options(section) Return list of configuration options for the named section. read(filenames, encoding=None) Read and parse the list of named configuration files, given by name. A single filename is also allowed. Non-existing files are ignored. Return list of successfully read files. read_file(f, filename=None) Read and parse one configuration file, given as a file object. The filename defaults to f.name; it is only used in error messages (if f has no `name' attribute, the string `<???>' is used). read_string(string) Read configuration from a given string. read_dict(dictionary) Read configuration from a dictionary. Keys are section names, values are dictionaries with keys and values that should be present in the section. If the used dictionary type preserves order, sections and their keys will be added in order. Values are automatically converted to strings. get(section, option, raw=False, vars=None, fallback=_UNSET) Return a string value for the named option. All % interpolations are expanded in the return values, based on the defaults passed into the constructor and the DEFAULT section. Additional substitutions may be provided using the `vars' argument, which must be a dictionary whose contents override any pre-existing defaults. If `option' is a key in `vars', the value from `vars' is used. getint(section, options, raw=False, vars=None, fallback=_UNSET) Like get(), but convert value to an integer. getfloat(section, options, raw=False, vars=None, fallback=_UNSET) Like get(), but convert value to a float. getboolean(section, options, raw=False, vars=None, fallback=_UNSET) Like get(), but convert value to a boolean (currently case insensitively defined as 0, false, no, off for False, and 1, true, yes, on for True). Returns False or True. items(section=_UNSET, raw=False, vars=None) If section is given, return a list of tuples with (name, value) for each option in the section. Otherwise, return a list of tuples with (section_name, section_proxy) for each section, including DEFAULTSECT. remove_section(section) Remove the given file section and all its options. remove_option(section, option) Remove the given option from the given section. set(section, option, value) Set the given option. write(fp, space_around_delimiters=True) Write the configuration state in .ini format. If `space_around_delimiters' is True (the default), delimiters between keys and values are surrounded by spaces. """
"""Generic socket server classes. This module tries to capture the various aspects of defining a server: For socket-based servers: - address family: - AF_INET{,6}: IP (Internet Protocol) sockets (default) - AF_UNIX: Unix domain sockets - others, e.g. AF_DECNET are conceivable (see <socket.h> - socket type: - SOCK_STREAM (reliable stream, e.g. TCP) - SOCK_DGRAM (datagrams, e.g. UDP) For request-based servers (including socket-based): - client address verification before further looking at the request (This is actually a hook for any processing that needs to look at the request before anything else, e.g. logging) - how to handle multiple requests: - synchronous (one request is handled at a time) - forking (each request is handled by a new process) - threading (each request is handled by a new thread) The classes in this module favor the server type that is simplest to write: a synchronous TCP/IP server. This is bad class design, but save some typing. (There's also the issue that a deep class hierarchy slows down method lookups.) There are five classes in an inheritance diagram, four of which represent synchronous servers of four types: +------------+ | BaseServer | +------------+ | v +-----------+ +------------------+ | TCPServer |------->| UnixStreamServer | +-----------+ +------------------+ | v +-----------+ +--------------------+ | UDPServer |------->| UnixDatagramServer | +-----------+ +--------------------+ Note that UnixDatagramServer derives from UDPServer, not from UnixStreamServer -- the only difference between an IP and a Unix stream server is the address family, which is simply repeated in both unix server classes. Forking and threading versions of each type of server can be created using the ForkingMixIn and ThreadingMixIn mix-in classes. For instance, a threading UDP server class is created as follows: class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass The Mix-in class must come first, since it overrides a method defined in UDPServer! Setting the various member variables also changes the behavior of the underlying server mechanism. To implement a service, you must derive a class from BaseRequestHandler and redefine its handle() method. You can then run various versions of the service by combining one of the server classes with your request handler class. The request handler class must be different for datagram or stream services. This can be hidden by using the request handler subclasses StreamRequestHandler or DatagramRequestHandler. Of course, you still have to use your head! For instance, it makes no sense to use a forking server if the service contains state in memory that can be modified by requests (since the modifications in the child process would never reach the initial state kept in the parent process and passed to each child). In this case, you can use a threading server, but you will probably have to use locks to avoid two requests that come in nearly simultaneous to apply conflicting changes to the server state. On the other hand, if you are building e.g. an HTTP server, where all data is stored externally (e.g. in the file system), a synchronous class will essentially render the service "deaf" while one request is being handled -- which may be for a very long time if a client is slow to read all the data it has requested. Here a threading or forking server is appropriate. In some cases, it may be appropriate to process part of a request synchronously, but to finish processing in a forked child depending on the request data. This can be implemented by using a synchronous server and doing an explicit fork in the request handler class handle() method. Another approach to handling multiple simultaneous requests in an environment that supports neither threads nor fork (or where these are too expensive or inappropriate for the service) is to maintain an explicit table of partially finished requests and to use select() to decide which request to work on next (or whether to handle a new incoming request). This is particularly important for stream services where each client can potentially be connected for a long time (if threads or subprocesses cannot be used). Future work: - Standard classes for Sun RPC (which uses either UDP or TCP) - Standard mix-in classes to implement various authentication and encryption schemes - Standard framework for select-based multiplexing XXX Open problems: - What to do with out-of-band data? BaseServer: - split generic "request" functionality out into BaseServer class. Copyright (C) 2000 NAME <EMAIL> example: read entries from a SQL database (requires overriding get_request() to return a table entry from the database). entry is processed by a RequestHandlerClass. """
""" ================================================= Orthogonal distance regression (:mod:`scipy.odr`) ================================================= .. currentmodule:: scipy.odr Package Content =============== .. autosummary:: :toctree: generated/ Data -- The data to fit. RealData -- Data with weights as actual std. dev.s and/or covariances. Model -- Stores information about the function to be fit. ODR -- Gathers all info & manages the main fitting routine. Output -- Result from the fit. odr -- Low-level function for ODR. OdrWarning -- Warning about potential problems when running ODR OdrError -- Error exception. OdrStop -- Stop exception. Prebuilt models: .. autosummary:: polynomial .. data:: exponential .. data:: multilinear .. data:: unilinear .. data:: quadratic .. data:: polynomial Usage information ================= Introduction ------------ Why Orthogonal Distance Regression (ODR)? Sometimes one has measurement errors in the explanatory (a.k.a., "independent") variable(s), not just the response (a.k.a., "dependent") variable(s). Ordinary Least Squares (OLS) fitting procedures treat the data for explanatory variables as fixed, i.e., not subject to error of any kind. Furthermore, OLS procedures require that the response variables be an explicit function of the explanatory variables; sometimes making the equation explicit is impractical and/or introduces errors. ODR can handle both of these cases with ease, and can even reduce to the OLS case if that is sufficient for the problem. ODRPACK is a FORTRAN-77 library for performing ODR with possibly non-linear fitting functions. It uses a modified trust-region Levenberg-Marquardt-type algorithm [1]_ to estimate the function parameters. The fitting functions are provided by Python functions operating on NumPy arrays. The required derivatives may be provided by Python functions as well, or may be estimated numerically. ODRPACK can do explicit or implicit ODR fits, or it can do OLS. Input and output variables may be multi-dimensional. Weights can be provided to account for different variances of the observations, and even covariances between dimensions of the variables. The `scipy.odr` package offers an object-oriented interface to ODRPACK, in addition to the low-level `odr` function. Additional background information about ODRPACK can be found in the `ODRPACK User's Guide <https://docs.scipy.org/doc/external/odrpack_guide.pdf>`_, reading which is recommended. Basic usage ----------- 1. Define the function you want to fit against.:: def f(B, x): '''Linear function y = m*x + b''' # B is a vector of the parameters. # x is an array of the current x values. # x is in the same format as the x passed to Data or RealData. # # Return an array in the same format as y passed to Data or RealData. return B[0]*x + B[1] 2. Create a Model.:: linear = Model(f) 3. Create a Data or RealData instance.:: mydata = Data(x, y, wd=1./power(sx,2), we=1./power(sy,2)) or, when the actual covariances are known:: mydata = RealData(x, y, sx=sx, sy=sy) 4. Instantiate ODR with your data, model and initial parameter estimate.:: myodr = ODR(mydata, linear, beta0=[1., 2.]) 5. Run the fit.:: myoutput = myodr.run() 6. Examine output.:: myoutput.pprint() References ---------- .. [1] NAME and NAME "Orthogonal Distance Regression," in "Statistical analysis of measurement error models and applications: proceedings of the AMS-IMS-SIAM joint summer research conference held June 10-16, 1989," Contemporary Mathematics, vol. 112, pg. 186, 1990. """
""" Copyright 2012, July 31 Written by NAME (Cheer) NAME EMAIL product in a grid Problem 11 In the 20 X 20 grid below, four numbers along a diagonal line have been marked in red. 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40 04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66 88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69 04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36 20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16 20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54 01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48 The product of these numbers is 26 63 78 14 = 1788696. What is the greatest product of four adjacent numbers in any direction (up, down, left, right, or diagonally) in the 20 X 20 grid? """
#Process: #1. Get a list of original sdf files #2. Use chopRDKit02.py generates fragments and list of files with total atom number, carbon atom number, nitrogen and oxygen atom number #3. Form lists of by atom numbers #4. Run rmRedLinker03.py or rmRedRigid01.py on different lists generated by step 3. Remove redundancy of linkers and rigids. #5. Remove temp file and dir. #main-script: # - eMolFrag.py #sub-scripts used: # - loader.py # - chopRDKit02.py, # - combineLinkers01.py # - rmRedRigid01.py, # - rmRedLinker03.py, # - mol-ali-04.py. #Usage: Read README file for detailed information. # 1. Configure path: python ConfigurePath.py # Path only need to be set before the first run if no changes to the paths. # 2. /Path_to_Python/python /Path_to_scripts/eMolFrag.py /Path_to_input_directory/ /Path_to_output_directory/ Number-Of-Cores #Args: # - /Path to Python/ ... Use python to run the script # - /Path to scripts/echop.py ... The directory of scripts and the name of the entrance to the software # - /Path to input directory/ ... The path to the input directory, in which is the input molecules in *.mol2 format # - /Path to output directory/ ... The path to the output directory, in which is the output files # - Number-Of-Cores ... Number of processings to be used in the run #Update Log: #This script is written by NAME Created 01/17/2016 - Chop # Modification 01/17/2016 - Remove bug # Modification 01/18/2016 - Reconnect linkers # Modification 01/21/2016 - Remove redundancy # Modification 02/29/2016 - Remove bug # Modification 03/16/2016 - Remove bug # Modification 03/17/2016 - Remove bug # Modification 03/24/2016 - Remove bug # Modification 03/25/2016 - Change each step to functions # Modification 04/03/2016 - Remove bug # Modification 04/06/2016 - Reduce temp output files # Modification 04/06/2016 - Remove bug # Modification 04/06/2016 - Start parallel with chop # Modification 04/17/2016 - Improve efficiency # Modification 04/18/2016 - Remove bug # Modification 05/24/2016 - Start parallel with remove redundancy # Modification 06/14/2016 - Add parallel option as arg # Modification 06/14/2016 - Remove bug # Modification 06/29/2016 - Remove bug # Modification 07/08/2016 - Change similarity criteria of rigids from 0.95 to 0.97 # Modification 07/11/2016 - Improve efficiency # Modification 07/18/2016 - Pack up, format. # Modification 07/19/2016 - Solve python 2.x/3.x compatibility problem. # Modification 07/20/2016 - Solve python 2.x/3.x compatibility problem. # Modification 07/21/2016 - Solve python 2.x/3.x compatibility problem. # Modification 07/22/2016 - Solve python 2.x/3.x compatibility problem. # Modification 07/22/2016 - Modify README file # Last revision 09/13/2016 - Solve output path conflict problem.
# Databricks notebook source # MAGIC %md # MAGIC ScaDaMaLe Course [site](https://lamastex.github.io/scalable-data-science/sds/3/x/) and [book](https://lamastex.github.io/ScaDaMaLe/index.html) # MAGIC # MAGIC This is a 2019-2021 augmentation and update of [Adam NAME initial notebooks. # MAGIC # MAGIC _Thanks to [Christian NAME and [William NAME for their contributions towards making these materials Spark 3.0.1 and Python 3+ compliant._ # COMMAND ---------- # MAGIC %md # MAGIC # Convolutional Neural Networks # MAGIC ## aka CNN, ConvNet # COMMAND ---------- # MAGIC %md # MAGIC As a baseline, let's start a lab running with what we already know. # MAGIC # MAGIC We'll take our deep feed-forward multilayer perceptron network, with ReLU activations and reasonable initializations, and apply it to learning the MNIST digits. # MAGIC # MAGIC The main part of the code looks like the following (full code you can run is in the next cell): # MAGIC # MAGIC ``` # MAGIC # imports, setup, load data sets # MAGIC # MAGIC model = Sequential() # MAGIC model.add(Dense(20, input_dim=784, kernel_initializer='normal', activation='relu')) # MAGIC model.add(Dense(15, kernel_initializer='normal', activation='relu')) # MAGIC model.add(Dense(10, kernel_initializer='normal', activation='softmax')) # MAGIC model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['categorical_accuracy']) # MAGIC # MAGIC categorical_labels = to_categorical(y_train, num_classes=10) # MAGIC # MAGIC history = model.fit(X_train, categorical_labels, epochs=100, batch_size=100) # MAGIC # MAGIC # print metrics, plot errors # MAGIC ``` # MAGIC # MAGIC Note the changes, which are largely about building a classifier instead of a regression model: # MAGIC * Output layer has one neuron per category, with softmax activation # MAGIC * __Loss function is cross-entropy loss__ # MAGIC * Accuracy metric is categorical accuracy # COMMAND ---------- # MAGIC %md # MAGIC Let's hold pointers into wikipedia for these new concepts. # COMMAND ---------- # MAGIC %scala # MAGIC //This allows easy embedding of publicly available information into any other notebook # MAGIC //Example usage: # MAGIC // displayHTML(frameIt("https://en.wikipedia.org/wiki/Latent_Dirichlet_allocation#Topics_in_LDA",250)) # MAGIC def frameIt( u:String, h:Int ) : String = { # MAGIC """<iframe # MAGIC src=""""+ u+"""" # MAGIC width="95%" height="""" + h + """" # MAGIC sandbox> # MAGIC <p> # MAGIC <a href="http://spark.apache.org/docs/latest/index.html"> # MAGIC Fallback link for browsers that, unlikely, don't support frames # MAGIC </a> # MAGIC </p> # MAGIC </iframe>""" # MAGIC } # MAGIC displayHTML(frameIt("https://en.wikipedia.org/wiki/Cross_entropy#Cross-entropy_error_function_and_logistic_regression",500)) # COMMAND ---------- # MAGIC %scala # MAGIC displayHTML(frameIt("https://en.wikipedia.org/wiki/Softmax_function",380)) # COMMAND ---------- # MAGIC %md # MAGIC The following is from: [https://www.quora.com/How-does-Keras-calculate-accuracy](https://www.quora.com/How-does-Keras-calculate-accuracy). # MAGIC # MAGIC **Categorical accuracy:** # MAGIC # MAGIC ```%python # MAGIC def categorical_accuracy(y_true, y_pred): # MAGIC return K.cast(K.equal(K.argmax(y_true, axis=-1), # MAGIC K.argmax(y_pred, axis=-1)), # MAGIC K.floatx()) # MAGIC ``` # MAGIC # MAGIC > `K.argmax(y_true)` takes the highest value to be the prediction and matches against the comparative set. # COMMAND ---------- # MAGIC %md # MAGIC Watch (1:39) # MAGIC * [![Udacity: Deep Learning by NAME - Cross-entropy](http://img.youtube.com/vi/tRsSi_sqXjI/0.jpg)](https://www.youtube.com/watch?v=tRsSi_sqXjI) # MAGIC # MAGIC Watch (1:54) # MAGIC * [![Udacity: Deep Learning by NAME - Minimizing Cross-entropy](http://img.youtube.com/vi/x449QQDhMDE/0.jpg)](https://www.youtube.com/watch?v=x449QQDhMDE) # COMMAND ----------
""" ================= Django S3 storage ================= Usage ===== Settings -------- ``DEFAULT_FILE_STORAGE`` ~~~~~~~~~~~~~~~~~~~~~~~~ This setting store the path to the S3 storage class, the first part correspond to the filepath and the second the name of the class, if you've got ``example.com`` in your ``PYTHONPATH`` and store your storage file in ``example.com/libs/storages/S3Storage.py``, the resulting setting will be:: DEFAULT_FILE_STORAGE = 'libs.storages.S3Storage.S3Storage' If you keep the same filename as in repository, it should always end with ``S3Storage.S3Storage``. ``AWS_ACCESS_KEY_ID`` ~~~~~~~~~~~~~~~~~~~~~ Your Amazon Web Services access key, as a string. ``AWS_SECRET_ACCESS_KEY`` ~~~~~~~~~~~~~~~~~~~~~~~~~ Your Amazon Web Services secret access key, as a string. ``AWS_STORAGE_BUCKET_NAME`` ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Your Amazon Web Services storage bucket name, as a string. ``AWS_CALLING_FORMAT`` ~~~~~~~~~~~~~~~~~~~~~~ The way you'd like to call the Amazon Web Services API, for instance if you prefer subdomains:: from S3 import CallingFormat AWS_CALLING_FORMAT = CallingFormat.SUBDOMAIN ``AWS_HEADERS`` (optionnal) ~~~~~~~~~~~~~~~~~~~~~~~~~~~ If you'd like to set headers sent with each file of the storage:: # see http://developer.yahoo.com/performance/rules.html#expires AWS_HEADERS = { 'Expires': 'Thu, 15 Apr 2010 20:00:00 GMT', 'Cache-Control': 'max-age=86400', } Fields ------ Once you're done, ``default_storage`` will be the S3 storage:: >>> from django.core.files.storage import default_storage >>> print default_storage.__class__ <class 'backends.S3Storage.S3Storage'> This way, if you define a new ``FileField``, it will use the S3 storage:: >>> from django.db import models >>> class Resume(models.Model): ... pdf = models.FileField(upload_to='pdfs') ... photos = models.ImageField(upload_to='photos') ... >>> resume = Resume() >>> print resume.pdf.storage <backends.S3Storage.S3Storage object at ...> Tests ===== Initialization:: >>> from django.core.files.storage import default_storage >>> from django.core.files.base import ContentFile >>> from django.core.cache import cache >>> from models import MyStorage Storage ------- Standard file access options are available, and work as expected:: >>> default_storage.exists('storage_test') False >>> file = default_storage.open('storage_test', 'w') >>> file.write('storage contents') >>> file.close() >>> default_storage.exists('storage_test') True >>> file = default_storage.open('storage_test', 'r') >>> file.read() 'storage contents' >>> file.close() >>> default_storage.delete('storage_test') >>> default_storage.exists('storage_test') False Model ----- An object without a file has limited functionality:: >>> obj1 = MyStorage() >>> obj1.normal <FieldFile: None> >>> obj1.normal.size Traceback (most recent call last): ... ValueError: The 'normal' attribute has no file associated with it. Saving a file enables full functionality:: >>> obj1.normal.save('django_test.txt', ContentFile('content')) >>> obj1.normal <FieldFile: tests/django_test.txt> >>> obj1.normal.size 7 >>> obj1.normal.read() 'content' Files can be read in a little at a time, if necessary:: >>> obj1.normal.open() >>> obj1.normal.read(3) 'con' >>> obj1.normal.read() 'tent' >>> '-'.join(obj1.normal.chunks(chunk_size=2)) 'co-nt-en-t' Save another file with the same name:: >>> obj2 = MyStorage() >>> obj2.normal.save('django_test.txt', ContentFile('more content')) >>> obj2.normal <FieldFile: tests/django_test_.txt> >>> obj2.normal.size 12 Push the objects into the cache to make sure they pickle properly:: >>> cache.set('obj1', obj1) >>> cache.set('obj2', obj2) >>> cache.get('obj2').normal <FieldFile: tests/django_test_.txt> Deleting an object deletes the file it uses, if there are no other objects still using that file:: >>> obj2.delete() >>> obj2.normal.save('django_test.txt', ContentFile('more content')) >>> obj2.normal <FieldFile: tests/django_test_.txt> Default values allow an object to access a single file:: >>> obj3 = MyStorage.objects.create() >>> obj3.default <FieldFile: tests/default.txt> >>> obj3.default.read() 'default content' But it shouldn't be deleted, even if there are no more objects using it:: >>> obj3.delete() >>> obj3 = MyStorage() >>> obj3.default.read() 'default content' Verify the fix for #5655, making sure the directory is only determined once:: >>> obj4 = MyStorage() >>> obj4.random.save('random_file', ContentFile('random content')) >>> obj4.random <FieldFile: .../random_file> Clean up the temporary files:: >>> obj1.normal.delete() >>> obj2.normal.delete() >>> obj3.default.delete() >>> obj4.random.delete() """
# -*- coding: utf-8 -*- # This file is part of ranger, the console file manager. # This configuration file is licensed under the same terms as ranger. # =================================================================== # # NOTE: If you copied this file to /etc/ranger/commands_full.py or # ~/.config/ranger/commands_full.py, then it will NOT be loaded by ranger, # and only serve as a reference. # # =================================================================== # This file contains ranger's commands. # It's all in python; lines beginning with # are comments. # # Note that additional commands are automatically generated from the methods # of the class ranger.core.actions.Actions. # # You can customize commands in the files /etc/ranger/commands.py (system-wide) # and ~/.config/ranger/commands.py (per user). # They have the same syntax as this file. In fact, you can just copy this # file to ~/.config/ranger/commands_full.py with # `ranger --copy-config=commands_full' and make your modifications, don't # forget to rename it to commands.py. You can also use # `ranger --copy-config=commands' to copy a short sample commands.py that # has everything you need to get started. # But make sure you update your configs when you update ranger. # # =================================================================== # Every class defined here which is a subclass of `Command' will be used as a # command in ranger. Several methods are defined to interface with ranger: # execute(): called when the command is executed. # cancel(): called when closing the console. # tab(tabnum): called when <TAB> is pressed. # quick(): called after each keypress. # # tab() argument tabnum is 1 for <TAB> and -1 for <S-TAB> by default # # The return values for tab() can be either: # None: There is no tab completion # A string: Change the console to this string # A list/tuple/generator: cycle through every item in it # # The return value for quick() can be: # False: Nothing happens # True: Execute the command afterwards # # The return value for execute() and cancel() doesn't matter. # # =================================================================== # Commands have certain attributes and methods that facilitate parsing of # the arguments: # # self.line: The whole line that was written in the console. # self.args: A list of all (space-separated) arguments to the command. # self.quantifier: If this command was mapped to the key "X" and # the user pressed 6X, self.quantifier will be 6. # self.arg(n): The n-th argument, or an empty string if it doesn't exist. # self.rest(n): The n-th argument plus everything that followed. For example, # if the command was "search foo bar a b c", rest(2) will be "bar a b c" # self.start(n): Anything before the n-th argument. For example, if the # command was "search foo bar a b c", start(2) will be "search foo" # # =================================================================== # And this is a little reference for common ranger functions and objects: # # self.fm: A reference to the "fm" object which contains most information # about ranger. # self.fm.notify(string): Print the given string on the screen. # self.fm.notify(string, bad=True): Print the given string in RED. # self.fm.reload_cwd(): Reload the current working directory. # self.fm.thisdir: The current working directory. (A File object.) # self.fm.thisfile: The current file. (A File object too.) # self.fm.thistab.get_selection(): A list of all selected files. # self.fm.execute_console(string): Execute the string as a ranger command. # self.fm.open_console(string): Open the console with the given string # already typed in for you. # self.fm.move(direction): Moves the cursor in the given direction, which # can be something like down=3, up=5, right=1, left=1, to=6, ... # # File objects (for example self.fm.thisfile) have these useful attributes and # methods: # # tfile.path: The path to the file. # tfile.basename: The base name only. # tfile.load_content(): Force a loading of the directories content (which # obviously works with directories only) # tfile.is_directory: True/False depending on whether it's a directory. # # For advanced commands it is unavoidable to dive a bit into the source code # of ranger. # ===================================================================
#!/usr/bin/python2 # SPDX-License-Identifier: GPL-2.0 # exported-sql-viewer.py: view data from sql database # Copyright (c) 2014-2018, Intel Corporation. # To use this script you will need to have exported data using either the # export-to-sqlite.py or the export-to-postgresql.py script. Refer to those # scripts for details. # # Following on from the example in the export scripts, a # call-graph can be displayed for the pt_example database like this: # # python tools/perf/scripts/python/exported-sql-viewer.py pt_example # # Note that for PostgreSQL, this script supports connecting to remote databases # by setting hostname, port, username, password, and dbname e.g. # # python tools/perf/scripts/python/exported-sql-viewer.py "hostname=myhost username=myuser password=mypassword dbname=pt_example" # # The result is a GUI window with a tree representing a context-sensitive # call-graph. Expanding a couple of levels of the tree and adjusting column # widths to suit will display something like: # # Call Graph: pt_example # Call Path Object Count Time(ns) Time(%) Branch Count Branch Count(%) # v- ls # v- 2638:2638 # v- _start ld-2.19.so 1 10074071 100.0 211135 100.0 # |- unknown unknown 1 13198 0.1 1 0.0 # >- _dl_start ld-2.19.so 1 1400980 13.9 19637 9.3 # >- _d_linit_internal ld-2.19.so 1 448152 4.4 11094 5.3 # v-__libc_start_main@plt ls 1 8211741 81.5 180397 85.4 # >- _dl_fixup ld-2.19.so 1 7607 0.1 108 0.1 # >- __cxa_atexit libc-2.19.so 1 11737 0.1 10 0.0 # >- __libc_csu_init ls 1 10354 0.1 10 0.0 # |- _setjmp libc-2.19.so 1 0 0.0 4 0.0 # v- main ls 1 8182043 99.6 180254 99.9 # # Points to note: # The top level is a command name (comm) # The next level is a thread (pid:tid) # Subsequent levels are functions # 'Count' is the number of calls # 'Time' is the elapsed time until the function returns # Percentages are relative to the level above # 'Branch Count' is the total number of branches for that function and all # functions that it calls # There is also a "All branches" report, which displays branches and # possibly disassembly. However, presently, the only supported disassembler is # Intel XED, and additionally the object code must be present in perf build ID # cache. To use Intel XED, libxed.so must be present. To build and install # libxed.so: # git clone https://github.com/intelxed/mbuild.git mbuild # git clone https://github.com/intelxed/xed # cd xed # ./mfile.py --share # sudo ./mfile.py --prefix=/usr/local install # sudo ldconfig # # Example report: # # Time CPU Command PID TID Branch Type In Tx Branch # 8107675239590 2 ls 22011 22011 return from interrupt No ffffffff86a00a67 native_irq_return_iret ([kernel]) -> 7fab593ea260 _start (ld-2.19.so) # 7fab593ea260 48 89 e7 mov %rsp, %rdi # 8107675239899 2 ls 22011 22011 hardware interrupt No 7fab593ea260 _start (ld-2.19.so) -> ffffffff86a012e0 page_fault ([kernel]) # 8107675241900 2 ls 22011 22011 return from interrupt No ffffffff86a00a67 native_irq_return_iret ([kernel]) -> 7fab593ea260 _start (ld-2.19.so) # 7fab593ea260 48 89 e7 mov %rsp, %rdi # 7fab593ea263 e8 c8 06 00 00 callq 0x7fab593ea930 # 8107675241900 2 ls 22011 22011 call No 7fab593ea263 _start+0x3 (ld-2.19.so) -> 7fab593ea930 _dl_start (ld-2.19.so) # 7fab593ea930 55 pushq %rbp # 7fab593ea931 48 89 e5 mov %rsp, %rbp # 7fab593ea934 41 57 pushq %r15 # 7fab593ea936 41 56 pushq %r14 # 7fab593ea938 41 55 pushq %r13 # 7fab593ea93a 41 54 pushq %r12 # 7fab593ea93c 53 pushq %rbx # 7fab593ea93d 48 89 fb mov %rdi, %rbx # 7fab593ea940 48 83 ec 68 sub $0x68, %rsp # 7fab593ea944 0f 31 rdtsc # 7fab593ea946 48 c1 e2 20 shl $0x20, %rdx # 7fab593ea94a 89 c0 mov %eax, %eax # 7fab593ea94c 48 09 c2 or %rax, %rdx # 7fab593ea94f 48 8b 05 1a 15 22 00 movq 0x22151a(%rip), %rax # 8107675242232 2 ls 22011 22011 hardware interrupt No 7fab593ea94f _dl_start+0x1f (ld-2.19.so) -> ffffffff86a012e0 page_fault ([kernel]) # 8107675242900 2 ls 22011 22011 return from interrupt No ffffffff86a00a67 native_irq_return_iret ([kernel]) -> 7fab593ea94f _dl_start+0x1f (ld-2.19.so) # 7fab593ea94f 48 8b 05 1a 15 22 00 movq 0x22151a(%rip), %rax # 7fab593ea956 48 89 15 3b 13 22 00 movq %rdx, 0x22133b(%rip) # 8107675243232 2 ls 22011 22011 hardware interrupt No 7fab593ea956 _dl_start+0x26 (ld-2.19.so) -> ffffffff86a012e0 page_fault ([kernel])
""" ======================== Broadcasting over arrays ======================== The term broadcasting describes how numpy treats arrays with different shapes during arithmetic operations. Subject to certain constraints, the smaller array is "broadcast" across the larger array so that they have compatible shapes. Broadcasting provides a means of vectorizing array operations so that looping occurs in C instead of Python. It does this without making needless copies of data and usually leads to efficient algorithm implementations. There are, however, cases where broadcasting is a bad idea because it leads to inefficient use of memory that slows computation. NumPy operations are usually done on pairs of arrays on an element-by-element basis. In the simplest case, the two arrays must have exactly the same shape, as in the following example: >>> a = np.array([1.0, 2.0, 3.0]) >>> b = np.array([2.0, 2.0, 2.0]) >>> a * b array([ 2., 4., 6.]) NumPy's broadcasting rule relaxes this constraint when the arrays' shapes meet certain constraints. The simplest broadcasting example occurs when an array and a scalar value are combined in an operation: >>> a = np.array([1.0, 2.0, 3.0]) >>> b = 2.0 >>> a * b array([ 2., 4., 6.]) The result is equivalent to the previous example where ``b`` was an array. We can think of the scalar ``b`` being *stretched* during the arithmetic operation into an array with the same shape as ``a``. The new elements in ``b`` are simply copies of the original scalar. The stretching analogy is only conceptual. NumPy is smart enough to use the original scalar value without actually making copies, so that broadcasting operations are as memory and computationally efficient as possible. The code in the second example is more efficient than that in the first because broadcasting moves less memory around during the multiplication (``b`` is a scalar rather than an array). General Broadcasting Rules ========================== When operating on two arrays, NumPy compares their shapes element-wise. It starts with the trailing dimensions, and works its way forward. Two dimensions are compatible when 1) they are equal, or 2) one of them is 1 If these conditions are not met, a ``ValueError: frames are not aligned`` exception is thrown, indicating that the arrays have incompatible shapes. The size of the resulting array is the maximum size along each dimension of the input arrays. Arrays do not need to have the same *number* of dimensions. For example, if you have a ``256x256x3`` array of RGB values, and you want to scale each color in the image by a different value, you can multiply the image by a one-dimensional array with 3 values. Lining up the sizes of the trailing axes of these arrays according to the broadcast rules, shows that they are compatible:: Image (3d array): 256 x 256 x 3 Scale (1d array): 3 Result (3d array): 256 x 256 x 3 When either of the dimensions compared is one, the other is used. In other words, dimensions with size 1 are stretched or "copied" to match the other. In the following example, both the ``A`` and ``B`` arrays have axes with length one that are expanded to a larger size during the broadcast operation:: A (4d array): 8 x 1 x 6 x 1 B (3d array): 7 x 1 x 5 Result (4d array): 8 x 7 x 6 x 5 Here are some more examples:: A (2d array): 5 x 4 B (1d array): 1 Result (2d array): 5 x 4 A (2d array): 5 x 4 B (1d array): 4 Result (2d array): 5 x 4 A (3d array): 15 x 3 x 5 B (3d array): 15 x 1 x 5 Result (3d array): 15 x 3 x 5 A (3d array): 15 x 3 x 5 B (2d array): 3 x 5 Result (3d array): 15 x 3 x 5 A (3d array): 15 x 3 x 5 B (2d array): 3 x 1 Result (3d array): 15 x 3 x 5 Here are examples of shapes that do not broadcast:: A (1d array): 3 B (1d array): 4 # trailing dimensions do not match A (2d array): 2 x 1 B (3d array): 8 x 4 x 3 # second from last dimensions mismatched An example of broadcasting in practice:: >>> x = np.arange(4) >>> xx = x.reshape(4,1) >>> y = np.ones(5) >>> z = np.ones((3,4)) >>> x.shape (4,) >>> y.shape (5,) >>> x + y <type 'exceptions.ValueError'>: shape mismatch: objects cannot be broadcast to a single shape >>> xx.shape (4, 1) >>> y.shape (5,) >>> (xx + y).shape (4, 5) >>> xx + y array([[ 1., 1., 1., 1., 1.], [ 2., 2., 2., 2., 2.], [ 3., 3., 3., 3., 3.], [ 4., 4., 4., 4., 4.]]) >>> x.shape (4,) >>> z.shape (3, 4) >>> (x + z).shape (3, 4) >>> x + z array([[ 1., 2., 3., 4.], [ 1., 2., 3., 4.], [ 1., 2., 3., 4.]]) Broadcasting provides a convenient way of taking the outer product (or any other outer operation) of two arrays. The following example shows an outer addition operation of two 1-d arrays:: >>> a = np.array([0.0, 10.0, 20.0, 30.0]) >>> b = np.array([1.0, 2.0, 3.0]) >>> a[:, np.newaxis] + b array([[ 1., 2., 3.], [ 11., 12., 13.], [ 21., 22., 23.], [ 31., 32., 33.]]) Here the ``newaxis`` index operator inserts a new axis into ``a``, making it a two-dimensional ``4x1`` array. Combining the ``4x1`` array with ``b``, which has shape ``(3,)``, yields a ``4x3`` array. See `this article <http://wiki.scipy.org/EricsBroadcastingDoc>`_ for illustrations of broadcasting concepts. """
"""Doctest for method/function calls. We're going the use these types for extra testing >>> from UserList import UserList >>> from UserDict import UserDict We're defining four helper functions >>> def e(a,b): ... print a, b >>> def f(*a, **k): ... print a, test_support.sortdict(k) >>> def g(x, *y, **z): ... print x, y, test_support.sortdict(z) >>> def h(j=1, a=2, h=3): ... print j, a, h Argument list examples >>> f() () {} >>> f(1) (1,) {} >>> f(1, 2) (1, 2) {} >>> f(1, 2, 3) (1, 2, 3) {} >>> f(1, 2, 3, *(4, 5)) (1, 2, 3, 4, 5) {} >>> f(1, 2, 3, *[4, 5]) (1, 2, 3, 4, 5) {} >>> f(1, 2, 3, *UserList([4, 5])) (1, 2, 3, 4, 5) {} Here we add keyword arguments >>> f(1, 2, 3, **{'a':4, 'b':5}) (1, 2, 3) {'a': 4, 'b': 5} >>> f(1, 2, 3, *[4, 5], **{'a':6, 'b':7}) (1, 2, 3, 4, 5) {'a': 6, 'b': 7} >>> f(1, 2, 3, x=4, y=5, *(6, 7), **{'a':8, 'b': 9}) (1, 2, 3, 6, 7) {'a': 8, 'b': 9, 'x': 4, 'y': 5} >>> f(1, 2, 3, **UserDict(a=4, b=5)) (1, 2, 3) {'a': 4, 'b': 5} >>> f(1, 2, 3, *(4, 5), **UserDict(a=6, b=7)) (1, 2, 3, 4, 5) {'a': 6, 'b': 7} >>> f(1, 2, 3, x=4, y=5, *(6, 7), **UserDict(a=8, b=9)) (1, 2, 3, 6, 7) {'a': 8, 'b': 9, 'x': 4, 'y': 5} Examples with invalid arguments (TypeErrors). We're also testing the function names in the exception messages. Verify clearing of SF bug #733667 >>> e(c=4) Traceback (most recent call last): ... TypeError: e() got an unexpected keyword argument 'c' >>> g() Traceback (most recent call last): ... TypeError: g() takes at least 1 argument (0 given) >>> g(*()) Traceback (most recent call last): ... TypeError: g() takes at least 1 argument (0 given) >>> g(*(), **{}) Traceback (most recent call last): ... TypeError: g() takes at least 1 argument (0 given) >>> g(1) 1 () {} >>> g(1, 2) 1 (2,) {} >>> g(1, 2, 3) 1 (2, 3) {} >>> g(1, 2, 3, *(4, 5)) 1 (2, 3, 4, 5) {} >>> class Nothing: pass ... >>> g(*Nothing()) Traceback (most recent call last): ... TypeError: g() argument after * must be a sequence, not instance >>> class Nothing: ... def __len__(self): return 5 ... >>> g(*Nothing()) Traceback (most recent call last): ... TypeError: g() argument after * must be a sequence, not instance >>> class Nothing(): ... def __len__(self): return 5 ... def __getitem__(self, i): ... if i<3: return i ... else: raise IndexError(i) ... >>> g(*Nothing()) 0 (1, 2) {} >>> class Nothing: ... def __init__(self): self.c = 0 ... def __iter__(self): return self ... def next(self): ... if self.c == 4: ... raise StopIteration ... c = self.c ... self.c += 1 ... return c ... >>> g(*Nothing()) 0 (1, 2, 3) {} Make sure that the function doesn't stomp the dictionary >>> d = {'a': 1, 'b': 2, 'c': 3} >>> d2 = d.copy() >>> g(1, d=4, **d) 1 () {'a': 1, 'b': 2, 'c': 3, 'd': 4} >>> d == d2 True What about willful misconduct? >>> def saboteur(**kw): ... kw['x'] = 'm' ... return kw >>> d = {} >>> kw = saboteur(a=1, **d) >>> d {} >>> g(1, 2, 3, **{'x': 4, 'y': 5}) Traceback (most recent call last): ... TypeError: g() got multiple values for keyword argument 'x' >>> f(**{1:2}) Traceback (most recent call last): ... TypeError: f() keywords must be strings >>> h(**{'e': 2}) Traceback (most recent call last): ... TypeError: h() got an unexpected keyword argument 'e' >>> h(*h) Traceback (most recent call last): ... TypeError: h() argument after * must be a sequence, not function >>> dir(*h) Traceback (most recent call last): ... TypeError: dir() argument after * must be a sequence, not function >>> None(*h) Traceback (most recent call last): ... TypeError: NoneType object argument after * must be a sequence, \ not function >>> h(**h) Traceback (most recent call last): ... TypeError: h() argument after ** must be a mapping, not function >>> dir(**h) Traceback (most recent call last): ... TypeError: dir() argument after ** must be a mapping, not function >>> None(**h) Traceback (most recent call last): ... TypeError: NoneType object argument after ** must be a mapping, \ not function >>> dir(b=1, **{'b': 1}) Traceback (most recent call last): ... TypeError: dir() got multiple values for keyword argument 'b' Another helper function >>> def f2(*a, **b): ... return a, b >>> d = {} >>> for i in xrange(512): ... key = 'k%d' % i ... d[key] = i >>> a, b = f2(1, *(2,3), **d) >>> len(a), len(b), b == d (3, 512, True) >>> class Foo: ... def method(self, arg1, arg2): ... return arg1+arg2 >>> x = Foo() >>> Foo.method(*(x, 1, 2)) 3 >>> Foo.method(x, *(1, 2)) 3 >>> Foo.method(*(1, 2, 3)) Traceback (most recent call last): ... TypeError: unbound method method() must be called with Foo instance as \ first argument (got int instance instead) >>> Foo.method(1, *[2, 3]) Traceback (most recent call last): ... TypeError: unbound method method() must be called with Foo instance as \ first argument (got int instance instead) A PyCFunction that takes only positional parameters shoud allow an empty keyword dictionary to pass without a complaint, but raise a TypeError if te dictionary is not empty >>> try: ... silence = id(1, *{}) ... True ... except: ... False True >>> id(1, **{'foo': 1}) Traceback (most recent call last): ... TypeError: id() takes no keyword arguments """
#!/usr/bin/env python ## ## @file rewrite_pydoc.py ## @brief Convert libSBML Python doc file to something readable as docstrings ## @author NAME Purpose: ## ## The comments in the libSBML source code use Doxygen mark-up; this ## content is read by Doxygen and Javadoc, in combination with other ## scripts, to produce the libSBML API documentation in the libSBML "docs" ## directory. When creating the Python language interface, SWIG takes the ## comments and inserts them as documentation strings in the Python code, ## which is good from the perspective of being an easy way to provide help ## for the Python interface classes and methods, but bad from the ## perspective that it is full of Doxygen markup and not suitable for ## direct reading by humans. ## ## This program converts the Doxygen-based documentation strings by ## rewriting them to plain text. This plain text can then be included in ## the final Python bindings for libSBML, so that users can use the Python ## interactive help system to view the documentation. ## ## This program is not a general converter; it is designed specifically to ## work with the way that we generate the libSBML python bindings. ## However, it should not be too difficult to adapt to other similar ## software projects. ## ## The main hardwired assumptions are the following: ## ## * The input file to rewrite_pydoc.py is the output produced by our ## ../../swig/swigdoc.py, which produces documentation definitions for ## swig. These have the form shown in the following example: ## ## %feature("docstring") SBMLReader::SBMLReader " ## Creates a new SBMLReader and returns it. ## ## The libSBML SBMLReader objects offer methods for reading SBML in ## XML form from files and text strings. ## "; ## ## The output of rewrite_pydoc.py is another .i file in which all Doxygen ## tags have been translated and the docstring contents have been ## reformatted for use in the python plain-text interactive help system. ## ## * In our process for producing the libSBML Python bindings, we take the ## output of rewrite_pydoc.py and include it in the input to swig. This ## is done via an %include command in the ../local.i file. The ## consequence is that swig reads these %feature commands, and uses them ## when it produces a file named "libsbml.py" containing the Python code ## for the libSBML interface. The objects and methods in "libsbml.py" ## contain Python-style "docstrings" that are a combination of we defined ## in the .i file and what swig itself constructs. (In particular, swig ## adds documentation about the method signatures, because the methods ## are interfaces to native code and Python introspection cannot reveal ## the data types of the parameters.) ## ## * The Doxygen markup understood by rewrite_pydoc.py is not the complete ## set of all possible Doxygen tags. We don't use all possible Doxygen ## tags in the libSBML documentation, and so this program only looks for ## the ones we have been using. ## ## ## Special features: ## ## * When looking for @htmlinclude files, it first checks to see if a ## version of the file with a .txt extension exists in the same location ## where it finds the .html file. If the .txt eversion exists, it ## includes that instead of the .html file. (This allows hand-formatted ## text files to be used, which is useful for files containing tables, ## because the Python HTML parser library doesn't handle tables.) ## ## * When expanding @image directives, it looks for a file with the ## extension .txt in the same directory where it finds the .jpg file. If ## the .txt version exists, it includes that; if it doesn't exist, it ## does not include anything. (Since the docstrings are plain-text, no ## other action seems sensible in this context.) ## ## ## <!-------------------------------------------------------------------------- ## This file is part of libSBML. Please visit http://sbml.org for more ## information about SBML, and the latest version of libSBML. ## ## Copyright (C) 2009-2013 jointly by the following organizations: ## 1. California Institute of Technology, Pasadena, CA, USA ## 2. EMBL European Bioinformatics Institute (EBML-EBI), Hinxton, UK ## ## Copyright (C) 2006-2008 by the California Institute of Technology, ## Pasadena, CA, USA ## ## Copyright (C) 2002-2005 jointly by the following organizations: ## 1. California Institute of Technology, Pasadena, CA, USA ## 2. Japan Science and Technology Agency, Japan ## ## This library is free software; you can redistribute it and/or modify it ## under the terms of the GNU Lesser General Public License as published by ## the Free Software Foundation. A copy of the license agreement is provided ## in the file named "LICENSE.txt" included with this software distribution ## and also available online as http://sbml.org/software/libsbml/license.html ## ------------------------------------------------------------------------ -->
""" ======================== Broadcasting over arrays ======================== The term broadcasting describes how numpy treats arrays with different shapes during arithmetic operations. Subject to certain constraints, the smaller array is "broadcast" across the larger array so that they have compatible shapes. Broadcasting provides a means of vectorizing array operations so that looping occurs in C instead of Python. It does this without making needless copies of data and usually leads to efficient algorithm implementations. There are, however, cases where broadcasting is a bad idea because it leads to inefficient use of memory that slows computation. NumPy operations are usually done on pairs of arrays on an element-by-element basis. In the simplest case, the two arrays must have exactly the same shape, as in the following example: >>> a = np.array([1.0, 2.0, 3.0]) >>> b = np.array([2.0, 2.0, 2.0]) >>> a * b array([ 2., 4., 6.]) NumPy's broadcasting rule relaxes this constraint when the arrays' shapes meet certain constraints. The simplest broadcasting example occurs when an array and a scalar value are combined in an operation: >>> a = np.array([1.0, 2.0, 3.0]) >>> b = 2.0 >>> a * b array([ 2., 4., 6.]) The result is equivalent to the previous example where ``b`` was an array. We can think of the scalar ``b`` being *stretched* during the arithmetic operation into an array with the same shape as ``a``. The new elements in ``b`` are simply copies of the original scalar. The stretching analogy is only conceptual. NumPy is smart enough to use the original scalar value without actually making copies, so that broadcasting operations are as memory and computationally efficient as possible. The code in the second example is more efficient than that in the first because broadcasting moves less memory around during the multiplication (``b`` is a scalar rather than an array). General Broadcasting Rules ========================== When operating on two arrays, NumPy compares their shapes element-wise. It starts with the trailing dimensions, and works its way forward. Two dimensions are compatible when 1) they are equal, or 2) one of them is 1 If these conditions are not met, a ``ValueError: frames are not aligned`` exception is thrown, indicating that the arrays have incompatible shapes. The size of the resulting array is the maximum size along each dimension of the input arrays. Arrays do not need to have the same *number* of dimensions. For example, if you have a ``256x256x3`` array of RGB values, and you want to scale each color in the image by a different value, you can multiply the image by a one-dimensional array with 3 values. Lining up the sizes of the trailing axes of these arrays according to the broadcast rules, shows that they are compatible:: Image (3d array): 256 x 256 x 3 Scale (1d array): 3 Result (3d array): 256 x 256 x 3 When either of the dimensions compared is one, the other is used. In other words, dimensions with size 1 are stretched or "copied" to match the other. In the following example, both the ``A`` and ``B`` arrays have axes with length one that are expanded to a larger size during the broadcast operation:: A (4d array): 8 x 1 x 6 x 1 B (3d array): 7 x 1 x 5 Result (4d array): 8 x 7 x 6 x 5 Here are some more examples:: A (2d array): 5 x 4 B (1d array): 1 Result (2d array): 5 x 4 A (2d array): 5 x 4 B (1d array): 4 Result (2d array): 5 x 4 A (3d array): 15 x 3 x 5 B (3d array): 15 x 1 x 5 Result (3d array): 15 x 3 x 5 A (3d array): 15 x 3 x 5 B (2d array): 3 x 5 Result (3d array): 15 x 3 x 5 A (3d array): 15 x 3 x 5 B (2d array): 3 x 1 Result (3d array): 15 x 3 x 5 Here are examples of shapes that do not broadcast:: A (1d array): 3 B (1d array): 4 # trailing dimensions do not match A (2d array): 2 x 1 B (3d array): 8 x 4 x 3 # second from last dimensions mismatched An example of broadcasting in practice:: >>> x = np.arange(4) >>> xx = x.reshape(4,1) >>> y = np.ones(5) >>> z = np.ones((3,4)) >>> x.shape (4,) >>> y.shape (5,) >>> x + y <type 'exceptions.ValueError'>: shape mismatch: objects cannot be broadcast to a single shape >>> xx.shape (4, 1) >>> y.shape (5,) >>> (xx + y).shape (4, 5) >>> xx + y array([[ 1., 1., 1., 1., 1.], [ 2., 2., 2., 2., 2.], [ 3., 3., 3., 3., 3.], [ 4., 4., 4., 4., 4.]]) >>> x.shape (4,) >>> z.shape (3, 4) >>> (x + z).shape (3, 4) >>> x + z array([[ 1., 2., 3., 4.], [ 1., 2., 3., 4.], [ 1., 2., 3., 4.]]) Broadcasting provides a convenient way of taking the outer product (or any other outer operation) of two arrays. The following example shows an outer addition operation of two 1-d arrays:: >>> a = np.array([0.0, 10.0, 20.0, 30.0]) >>> b = np.array([1.0, 2.0, 3.0]) >>> a[:, np.newaxis] + b array([[ 1., 2., 3.], [ 11., 12., 13.], [ 21., 22., 23.], [ 31., 32., 33.]]) Here the ``newaxis`` index operator inserts a new axis into ``a``, making it a two-dimensional ``4x1`` array. Combining the ``4x1`` array with ``b``, which has shape ``(3,)``, yields a ``4x3`` array. See `this article <http://wiki.scipy.org/EricsBroadcastingDoc>`_ for illustrations of broadcasting concepts. """
""" Introduction ============ SqlSoup provides a convenient way to access existing database tables without having to declare table or mapper classes ahead of time. It is built on top of the SQLAlchemy ORM and provides a super-minimalistic interface to an existing database. SqlSoup effectively provides a coarse grained, alternative interface to working with the SQLAlchemy ORM, providing a "self configuring" interface for extremely rudimental operations. It's somewhat akin to a "super novice mode" version of the ORM. While SqlSoup can be very handy, users are strongly encouraged to use the full ORM for non-trivial applications. Suppose we have a database with users, books, and loans tables (corresponding to the PyWebOff dataset, if you're curious). Creating a SqlSoup gateway is just like creating an SQLAlchemy engine:: >>> from sqlalchemy.ext.sqlsoup import SqlSoup >>> db = SqlSoup('sqlite:///:memory:') or, you can re-use an existing engine:: >>> db = SqlSoup(engine) You can optionally specify a schema within the database for your SqlSoup:: >>> db.schema = myschemaname Loading objects =============== Loading objects is as easy as this:: >>> users = db.users.all() >>> users.sort() >>> users [MappedUsers(name=u'Joe NAME MappedUsers(name=u'Bhargan Basepair',email=u'EMAIL',password=u'basepair',classname=None,admin=1)] Of course, letting the database do the sort is better:: >>> db.users.order_by(db.users.name).all() [MappedUsers(name=u'Bhargan Basepair',email=u'EMAIL',password=u'basepair',classname=None,admin=1), MappedUsers(name=u'Joe NAME access is intuitive:: >>> users[0].email u'EMAIL' Of course, you don't want to load all users very often. Let's add a WHERE clause. Let's also switch the order_by to DESC while we're at it:: >>> from sqlalchemy import or_, and_, desc >>> where = or_(db.users.name=='Bhargan Basepair', db.users.email=='EMAIL') >>> db.users.filter(where).order_by(desc(db.users.name)).all() [MappedUsers(name=u'Joe NAME MappedUsers(name=u'Bhargan NAME can also use .first() (to retrieve only the first object from a query) or .one() (like .first when you expect exactly one user -- it will raise an exception if more were returned):: >>> db.users.filter(db.users.name=='Bhargan NAME MappedUsers(name=u'Bhargan NAME name is the primary key, this is equivalent to >>> db.users.get('Bhargan NAME MappedUsers(name=u'Bhargan NAME is also equivalent to >>> db.users.filter_by(name='Bhargan NAME MappedUsers(name=u'Bhargan NAME is like filter, but takes kwargs instead of full clause expressions. This makes it more concise for simple queries like this, but you can't do complex queries like the or\_ above or non-equality based comparisons this way. Full query documentation ------------------------ Get, filter, filter_by, order_by, limit, and the rest of the query methods are explained in detail in :ref:`ormtutorial_querying`. Modifying objects ================= Modifying objects is intuitive:: >>> user = _ >>> user.email = 'EMAIL' >>> db.commit() (SqlSoup leverages the sophisticated SQLAlchemy unit-of-work code, so multiple updates to a single object will be turned into a single ``UPDATE`` statement when you commit.) To finish covering the basics, let's insert a new loan, then delete it:: >>> book_id = db.books.filter_by(title='Regional Variation in Moss').first().id >>> db.loans.insert(book_id=book_id, user_name=user.name) MappedLoans(book_id=2,user_name=u'Bhargan NAME >>> loan = db.loans.filter_by(book_id=2, user_name='Bhargan NAME >>> db.delete(loan) >>> db.commit() You can also delete rows that have not been loaded as objects. Let's do our insert/delete cycle once more, this time using the loans table's delete method. (For SQLAlchemy experts: note that no flush() call is required since this delete acts at the SQL level, not at the Mapper level.) The same where-clause construction rules apply here as to the select methods. :: >>> db.loans.insert(book_id=book_id, user_name=user.name) MappedLoans(book_id=2,user_name=u'Bhargan NAME >>> db.loans.delete(db.loans.book_id==2) You can similarly update multiple rows at once. This will change the book_id to 1 in all loans whose book_id is 2:: >>> db.loans.update(db.loans.book_id==2, book_id=1) >>> db.loans.filter_by(book_id=1).all() [MappedLoans(book_id=1,user_name=u'Joe NAME 7, 12, 0, 0))] Joins ===== Occasionally, you will want to pull out a lot of data from related tables all at once. In this situation, it is far more efficient to have the database perform the necessary join. (Here we do not have *a lot of data* but hopefully the concept is still clear.) SQLAlchemy is smart enough to recognize that loans has a foreign key to users, and uses that as the join condition automatically. :: >>> join1 = db.join(db.users, db.loans, isouter=True) >>> join1.filter_by(name='Joe NAME [MappedJoin(name=u'Joe NAME0,book_id=1,user_name=u'Joe NAME 7, 12, 0, 0))] If you're unfortunate enough to be using MySQL with the default MyISAM storage engine, you'll have to specify the join condition manually, since MyISAM does not store foreign keys. Here's the same join again, with the join condition explicitly specified:: >>> db.join(db.users, db.loans, db.users.name==db.loans.user_name, isouter=True) <class 'sqlalchemy.ext.sqlsoup.MappedJoin'> You can compose arbitrarily complex joins by combining Join objects with tables or other joins. Here we combine our first join with the books table:: >>> join2 = db.join(join1, db.books) >>> join2.all() [MappedJoin(name=u'Joe NAME0,book_id=1,user_name=u'Joe NAME 7, 12, 0, 0),id=1,title=u'Mustards I Have Known',published_year=u'1989',authors=u'Jones')] If you join tables that have an identical column name, wrap your join with `with_labels`, to disambiguate columns with their table name (.c is short for .columns):: >>> db.with_labels(join1).c.keys() [u'users_name', u'users_email', u'users_password', u'users_classname', u'users_admin', u'loans_book_id', u'loans_user_name', u'loans_loan_date'] You can also join directly to a labeled object:: >>> labeled_loans = db.with_labels(db.loans) >>> db.join(db.users, labeled_loans, isouter=True).c.keys() [u'name', u'email', u'password', u'classname', u'admin', u'loans_book_id', u'loans_user_name', u'loans_loan_date'] Relationships ============= You can define relationships on SqlSoup classes: >>> db.users.relate('loans', db.loans) These can then be used like a normal SA property: >>> db.users.get('Joe Student').loans [MappedLoans(book_id=1,user_name=u'Joe NAME 7, 12, 0, 0))] >>> db.users.filter(~db.users.loans.any()).all() [MappedUsers(name=u'Bhargan NAME can take any options that the relationship function accepts in normal mapper definition: >>> del db._cache['users'] >>> db.users.relate('loans', db.loans, order_by=db.loans.loan_date, cascade='all, delete-orphan') Advanced Use ============ Sessions, Transations and Application Integration ------------------------------------------------- **Note:** please read and understand this section thoroughly before using SqlSoup in any web application. SqlSoup uses a ScopedSession to provide thread-local sessions. You can get a reference to the current one like this:: >>> session = db.session The default session is available at the module level in SQLSoup, via:: >>> from sqlalchemy.ext.sqlsoup import Session The configuration of this session is ``autoflush=True``, ``autocommit=False``. This means when you work with the SqlSoup object, you need to call ``db.commit()`` in order to have changes persisted. You may also call ``db.rollback()`` to roll things back. Since the SqlSoup object's Session automatically enters into a transaction as soon as it's used, it is *essential* that you call ``commit()`` or ``rollback()`` on it when the work within a thread completes. This means all the guidelines for web application integration at :ref:`session_lifespan` must be followed. The SqlSoup object can have any session or scoped session configured onto it. This is of key importance when integrating with existing code or frameworks such as Pylons. If your application already has a ``Session`` configured, pass it to your SqlSoup object:: >>> from myapplication import Session >>> db = SqlSoup(session=Session) If the ``Session`` is configured with ``autocommit=True``, use ``flush()`` instead of ``commit()`` to persist changes - in this case, the ``Session`` closes out its transaction immediately and no external management is needed. ``rollback()`` is also not available. Configuring a new SQLSoup object in "autocommit" mode looks like:: >>> from sqlalchemy.orm import scoped_session, sessionmaker >>> db = SqlSoup('sqlite://', session=scoped_session(sessionmaker(autoflush=False, expire_on_commit=False, autocommit=True))) Mapping arbitrary Selectables ----------------------------- SqlSoup can map any SQLAlchemy ``Selectable`` with the map method. Let's map a ``Select`` object that uses an aggregate function; we'll use the SQLAlchemy ``Table`` that SqlSoup introspected as the basis. (Since we're not mapping to a simple table or join, we need to tell SQLAlchemy how to find the *primary key* which just needs to be unique within the select, and not necessarily correspond to a *real* PK in the database.) :: >>> from sqlalchemy import select, func >>> b = db.books._table >>> s = select([b.c.published_year, func.count('*').label('n')], from_obj=[b], group_by=[b.c.published_year]) >>> s = s.alias('years_with_count') >>> years_with_count = db.map(s, primary_key=[s.c.published_year]) >>> years_with_count.filter_by(published_year='1989').all() [MappedBooks(published_year=u'1989',n=1)] Obviously if we just wanted to get a list of counts associated with book years once, raw SQL is going to be less work. The advantage of mapping a Select is reusability, both standalone and in Joins. (And if you go to full SQLAlchemy, you can perform mappings like this directly to your object models.) An easy way to save mapped selectables like this is to just hang them on your db object:: >>> db.years_with_count = years_with_count Python is flexible like that! Raw SQL ------- SqlSoup works fine with SQLAlchemy's text construct, described in :ref:`sqlexpression_text`. You can also execute textual SQL directly using the `execute()` method, which corresponds to the `execute()` method on the underlying `Session`. Expressions here are expressed like ``text()`` constructs, using named parameters with colons:: >>> rp = db.execute('select name, email from users where name like :name order by name', name='%Bhargan%') >>> for name, email in rp.fetchall(): print name, email Bhargan Basepair EMAIL you can get at the current transaction's connection using `connection()`. This is the raw connection object which can accept any sort of SQL expression or raw SQL string passed to the database:: >>> conn = db.connection() >>> conn.execute("'select name, email from users where name like ? order by name'", '%Bhargan%') Dynamic table names ------------------- You can load a table whose name is specified at runtime with the entity() method: >>> tablename = 'loans' >>> db.entity(tablename) == db.loans True entity() also takes an optional schema argument. If none is specified, the default schema is used. """
""" A class for converting a PySB model to a set of ordinary differential equations for integration in MATLAB. Note that for use in MATLAB, the name of the ``.m`` file must match the name of the exported MATLAB class (e.g., ``robertson.m`` for the example below). For information on how to use the model exporters, see the documentation for :py:mod:`pysb.export`. Output for the Robertson example model ====================================== Information on the form and usage of the generated MATLAB class is contained in the documentation for the MATLAB model, as shown in the following example for ``pysb.examples.robertson``:: classdef robertson % A simple three-species chemical kinetics system known as "Robertson's % example", as presented in: % % NAME The solution of a set of reaction rate equations, in Numerical % Analysis: An Introduction, NAME ed., Academic Press, 1966, pp. 178-182. % % A class implementing the ordinary differential equations % for the robertson model. % % Save as robertson.m. % % Generated by pysb.export.matlab.MatlabExporter. % % Properties % ---------- % observables : struct % A struct containing the names of the observables from the % PySB model as field names. Each field in the struct % maps the observable name to a matrix with two rows: % the first row specifies the indices of the species % associated with the observable, and the second row % specifies the coefficients associated with the species. % For any given timecourse of model species resulting from % integration, the timecourse for an observable can be % retrieved using the get_observable method, described % below. % % parameters : struct % A struct containing the names of the parameters from the % PySB model as field names. The nominal values are set by % the constructor and their values can be overriden % explicitly once an instance has been created. % % Methods % ------- % robertson.odes(tspan, y0) % The right-hand side function for the ODEs of the model, % for use with MATLAB ODE solvers (see Examples). % % robertson.get_initial_values() % Returns a vector of initial values for all species, % specified in the order that they occur in the original % PySB model (i.e., in the order found in model.species). % Non-zero initial conditions are specified using the % named parameters included as properties of the instance. % Hence initial conditions other than the defaults can be % used by assigning a value to the named parameter and then % calling this method. The vector returned by the method % is used for integration by passing it to the MATLAB % solver as the y0 argument. % % robertson.get_observables(y) % Given a matrix of timecourses for all model species % (i.e., resulting from an integration of the model), % get the trajectories corresponding to the observables. % Timecourses are returned as a struct which can be % indexed by observable name. % % Examples % -------- % Example integration using default initial and parameter % values: % % >> m = robertson(); % >> tspan = [0 100]; % >> [t y] = ode15s(@m.odes, tspan, m.get_initial_values()); % % Retrieving the observables: % % >> y_obs = m.get_observables(y) % properties observables parameters end methods function self = robertson() % Assign default parameter values self.parameters = struct( ... 'k1', 0.040000000000000001, ... 'k2', 30000000, ... 'k3', 10000, ... 'A_0', 1, ... 'B_0', 0, ... 'C_0', 0); % Define species indices (first row) and coefficients % (second row) of named observables self.observables = struct( ... 'A_total', [1; 1], ... 'B_total', [2; 1], ... 'C_total', [3; 1]); end function initial_values = get_initial_values(self) % Return the vector of initial conditions for all % species based on the values of the parameters % as currently defined in the instance. initial_values = zeros(1,3); initial_values(1) = self.parameters.A_0; % A() initial_values(2) = self.parameters.B_0; % B() initial_values(3) = self.parameters.C_0; % C() end function y = odes(self, tspan, y0) % Right hand side function for the ODEs % Shorthand for the struct of model parameters p = self.parameters; % A(); y(1,1) = -p.k1*y0(1) + p.k3*y0(2)*y0(3); % B(); y(2,1) = p.k1*y0(1) - p.k2*power(y0(2), 2) - p.k3*y0(2)*y0(3); % C(); y(3,1) = p.k2*power(y0(2), 2); end function y_obs = get_observables(self, y) % Retrieve the trajectories for the model observables % from a matrix of the trajectories of all model % species. % Initialize the struct of observable timecourses % that we will return y_obs = struct(); % Iterate over the observables; observable_names = fieldnames(self.observables); for i = 1:numel(observable_names) obs_matrix = self.observables.(observable_names{i}); species = obs_matrix(1, :); coefficients = obs_matrix(2, :); y_obs.(observable_names{i}) = ... y(:, species) * coefficients'; end end end end """
"""Generic socket server classes. This module tries to capture the various aspects of defining a server: For socket-based servers: - address family: - AF_INET{,6}: IP (Internet Protocol) sockets (default) - AF_UNIX: Unix domain sockets - others, e.g. AF_DECNET are conceivable (see <socket.h> - socket type: - SOCK_STREAM (reliable stream, e.g. TCP) - SOCK_DGRAM (datagrams, e.g. UDP) For request-based servers (including socket-based): - client address verification before further looking at the request (This is actually a hook for any processing that needs to look at the request before anything else, e.g. logging) - how to handle multiple requests: - synchronous (one request is handled at a time) - forking (each request is handled by a new process) - threading (each request is handled by a new thread) The classes in this module favor the server type that is simplest to write: a synchronous TCP/IP server. This is bad class design, but save some typing. (There's also the issue that a deep class hierarchy slows down method lookups.) There are five classes in an inheritance diagram, four of which represent synchronous servers of four types: +------------+ | BaseServer | +------------+ | v +-----------+ +------------------+ | TCPServer |------->| UnixStreamServer | +-----------+ +------------------+ | v +-----------+ +--------------------+ | UDPServer |------->| UnixDatagramServer | +-----------+ +--------------------+ Note that UnixDatagramServer derives from UDPServer, not from UnixStreamServer -- the only difference between an IP and a Unix stream server is the address family, which is simply repeated in both unix server classes. Forking and threading versions of each type of server can be created using the ForkingMixIn and ThreadingMixIn mix-in classes. For instance, a threading UDP server class is created as follows: class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass The Mix-in class must come first, since it overrides a method defined in UDPServer! Setting the various member variables also changes the behavior of the underlying server mechanism. To implement a service, you must derive a class from BaseRequestHandler and redefine its handle() method. You can then run various versions of the service by combining one of the server classes with your request handler class. The request handler class must be different for datagram or stream services. This can be hidden by using the request handler subclasses StreamRequestHandler or DatagramRequestHandler. Of course, you still have to use your head! For instance, it makes no sense to use a forking server if the service contains state in memory that can be modified by requests (since the modifications in the child process would never reach the initial state kept in the parent process and passed to each child). In this case, you can use a threading server, but you will probably have to use locks to avoid two requests that come in nearly simultaneous to apply conflicting changes to the server state. On the other hand, if you are building e.g. an HTTP server, where all data is stored externally (e.g. in the file system), a synchronous class will essentially render the service "deaf" while one request is being handled -- which may be for a very long time if a client is slow to reqd all the data it has requested. Here a threading or forking server is appropriate. In some cases, it may be appropriate to process part of a request synchronously, but to finish processing in a forked child depending on the request data. This can be implemented by using a synchronous server and doing an explicit fork in the request handler class handle() method. Another approach to handling multiple simultaneous requests in an environment that supports neither threads nor fork (or where these are too expensive or inappropriate for the service) is to maintain an explicit table of partially finished requests and to use select() to decide which request to work on next (or whether to handle a new incoming request). This is particularly important for stream services where each client can potentially be connected for a long time (if threads or subprocesses cannot be used). Future work: - Standard classes for Sun RPC (which uses either UDP or TCP) - Standard mix-in classes to implement various authentication and encryption schemes - Standard framework for select-based multiplexing XXX Open problems: - What to do with out-of-band data? BaseServer: - split generic "request" functionality out into BaseServer class. Copyright (C) 2000 NAME <EMAIL> example: read entries from a SQL database (requires overriding get_request() to return a table entry from the database). entry is processed by a RequestHandlerClass. """
"""ctypes-based OpenGL wrapper for Python This is the PyOpenGL 3.x tree, it attempts to provide a largely compatible API for code written with the PyOpenGL 2.x series using the ctypes foreign function interface system. Configuration Variables: There are a few configuration variables in this top-level module. Applications should be the only code that tweaks these variables, mid-level libraries should not take it upon themselves to disable/enable features at this level. The implication there is that your library code should be able to work with any of the valid configurations available with these sets of flags. Further, once any entry point has been loaded, the variables can no longer be updated. The OpenGL._confligflags module imports the variables from this location, and once that import occurs the flags should no longer be changed. ERROR_CHECKING -- if set to a False value before importing any OpenGL.* libraries will completely disable error-checking. This can dramatically improve performance, but makes debugging far harder. This is intended to be turned off *only* in a production environment where you *know* that your code is entirely free of situations where you use exception-handling to handle error conditions, i.e. where you are explicitly checking for errors everywhere they can occur in your code. Default: True ERROR_LOGGING -- If True, then wrap array-handler functions with error-logging operations so that all exceptions will be reported to log objects in OpenGL.logs, note that this means you will get lots of error logging whenever you have code that tests by trying something and catching an error, this is intended to be turned on only during development so that you can see why something is failing. Errors are normally logged to the OpenGL.errors logger. Only triggers if ERROR_CHECKING is True Default: False ERROR_ON_COPY -- if set to a True value before importing the numpy/lists support modules, will cause array operations to raise OpenGL.error.CopyError if the operation would cause a data-copy in order to make the passed data-type match the target data-type. This effectively disables all list/tuple array support, as they are inherently copy-based. This feature allows for optimisation of your application. It should only be enabled during testing stages to prevent raising errors on recoverable conditions at run-time. Default: False CONTEXT_CHECKING -- if set to True, PyOpenGL will wrap *every* GL and GLU call with a check to see if there is a valid context. If there is no valid context then will throw OpenGL.errors.NoContext. This is an *extremely* slow check and is not enabled by default, intended to be enabled in order to track down (wrong) code that uses GL/GLU entry points before the context has been initialized (something later Linux GLs are very picky about). Default: False STORE_POINTERS -- if set to True, PyOpenGL array operations will attempt to store references to pointers which are being passed in order to prevent memory-access failures if the pointed-to-object goes out of scope. This behaviour is primarily intended to allow temporary arrays to be created without causing memory errors, thus it is trading off performance for safety. To use this flag effectively, you will want to first set ERROR_ON_COPY to True and eliminate all cases where you are copying arrays. Copied arrays *will* segfault your application deep within the GL if you disable this feature! Once you have eliminated all copying of arrays in your application, you will further need to be sure that all arrays which are passed to the GL are stored for at least the time period for which they are active in the GL. That is, you must be sure that your array objects live at least until they are no longer bound in the GL. This is something you need to confirm by thinking about your application's structure. When you are sure your arrays won't cause seg-faults, you can set STORE_POINTERS=False in your application and enjoy a (slight) speed up. Note: this flag is *only* observed when ERROR_ON_COPY == True, as a safety measure to prevent pointless segfaults Default: True WARN_ON_FORMAT_UNAVAILABLE -- If True, generates logging-module warn-level events when a FormatHandler plugin is not loadable (with traceback). Default: False FULL_LOGGING -- If True, then wrap functions with logging operations which reports each call along with its arguments to the OpenGL.calltrace logger at the INFO level. This is *extremely* slow. You should *not* enable this in production code! You will need to have a logging configuration (e.g. logging.basicConfig() ) call in your top-level script to see the results of the logging. Default: False ALLOW_NUMPY_SCALARS -- if True, we will wrap all GLint/GLfloat calls conversions with wrappers that allow for passing numpy scalar values. Note that this is experimental, *not* reliable, and very slow! Note that byte/char types are not wrapped. Default: False UNSIGNED_BYTE_IMAGES_AS_STRING -- if True, we will return GL_UNSIGNED_BYTE image-data as strings, instead of arrays for glReadPixels and glGetTexImage Default: True FORWARD_COMPATIBLE_ONLY -- only include OpenGL 3.1 compatible entry points. Note that this will generally break most PyOpenGL code that hasn't been explicitly made "legacy free" via a significant rewrite. Default: False SIZE_1_ARRAY_UNPACK -- if True, unpack size-1 arrays to be scalar values, as done in PyOpenGL 1.5 -> 3.0.0, that is, if a glGenList( 1 ) is done, return a uint rather than an array of uints. Default: True USE_ACCELERATE -- if True, attempt to use the OpenGL_accelerate package to provide Cython-coded accelerators for core wrapping operations. Default: True MODULE_ANNOTATIONS -- if True, attempt to annotate alternates() and constants to track in which module they are defined (only useful for the documentation-generation passes, really). Default: False """
""" ============================= Byteswapping and byte order ============================= Introduction to byte ordering and ndarrays ========================================== The ``ndarray`` is an object that provide a python array interface to data in memory. It often happens that the memory that you want to view with an array is not of the same byte ordering as the computer on which you are running Python. For example, I might be working on a computer with a little-endian CPU - such as an Intel Pentium, but I have loaded some data from a file written by a computer that is big-endian. Let's say I have loaded 4 bytes from a file written by a Sun (big-endian) computer. I know that these 4 bytes represent two 16-bit integers. On a big-endian machine, a two-byte integer is stored with the Most Significant Byte (MSB) first, and then the Least Significant Byte (LSB). Thus the bytes are, in memory order: #. MSB integer 1 #. LSB integer 1 #. MSB integer 2 #. LSB integer 2 Let's say the two integers were in fact 1 and 770. Because 770 = 256 * 3 + 2, the 4 bytes in memory would contain respectively: 0, 1, 3, 2. The bytes I have loaded from the file would have these contents: >>> big_end_str = chr(0) + chr(1) + chr(3) + chr(2) >>> big_end_str '\\x00\\x01\\x03\\x02' We might want to use an ``ndarray`` to access these integers. In that case, we can create an array around this memory, and tell numpy that there are two integers, and that they are 16 bit and big-endian: >>> import numpy as np >>> big_end_arr = np.ndarray(shape=(2,),dtype='>i2', buffer=big_end_str) >>> big_end_arr[0] 1 >>> big_end_arr[1] 770 Note the array ``dtype`` above of ``>i2``. The ``>`` means 'big-endian' (``<`` is little-endian) and ``i2`` means 'signed 2-byte integer'. For example, if our data represented a single unsigned 4-byte little-endian integer, the dtype string would be ``<u4``. In fact, why don't we try that? >>> little_end_u4 = np.ndarray(shape=(1,),dtype='<u4', buffer=big_end_str) >>> little_end_u4[0] == 1 * 256**1 + 3 * 256**2 + 2 * 256**3 True Returning to our ``big_end_arr`` - in this case our underlying data is big-endian (data endianness) and we've set the dtype to match (the dtype is also big-endian). However, sometimes you need to flip these around. .. warning:: Scalars currently do not include byte order information, so extracting a scalar from an array will return an integer in native byte order. Hence: >>> big_end_arr[0].dtype.byteorder == little_end_u4[0].dtype.byteorder True Changing byte ordering ====================== As you can imagine from the introduction, there are two ways you can affect the relationship between the byte ordering of the array and the underlying memory it is looking at: * Change the byte-ordering information in the array dtype so that it interprets the underlying data as being in a different byte order. This is the role of ``arr.newbyteorder()`` * Change the byte-ordering of the underlying data, leaving the dtype interpretation as it was. This is what ``arr.byteswap()`` does. The common situations in which you need to change byte ordering are: #. Your data and dtype endianess don't match, and you want to change the dtype so that it matches the data. #. Your data and dtype endianess don't match, and you want to swap the data so that they match the dtype #. Your data and dtype endianess match, but you want the data swapped and the dtype to reflect this Data and dtype endianness don't match, change dtype to match data ----------------------------------------------------------------- We make something where they don't match: >>> wrong_end_dtype_arr = np.ndarray(shape=(2,),dtype='<i2', buffer=big_end_str) >>> wrong_end_dtype_arr[0] 256 The obvious fix for this situation is to change the dtype so it gives the correct endianness: >>> fixed_end_dtype_arr = wrong_end_dtype_arr.newbyteorder() >>> fixed_end_dtype_arr[0] 1 Note the array has not changed in memory: >>> fixed_end_dtype_arr.tobytes() == big_end_str True Data and type endianness don't match, change data to match dtype ---------------------------------------------------------------- You might want to do this if you need the data in memory to be a certain ordering. For example you might be writing the memory out to a file that needs a certain byte ordering. >>> fixed_end_mem_arr = wrong_end_dtype_arr.byteswap() >>> fixed_end_mem_arr[0] 1 Now the array *has* changed in memory: >>> fixed_end_mem_arr.tobytes() == big_end_str False Data and dtype endianness match, swap data and dtype ---------------------------------------------------- You may have a correctly specified array dtype, but you need the array to have the opposite byte order in memory, and you want the dtype to match so the array values make sense. In this case you just do both of the previous operations: >>> swapped_end_arr = big_end_arr.byteswap().newbyteorder() >>> swapped_end_arr[0] 1 >>> swapped_end_arr.tobytes() == big_end_str False An easier way of casting the data to a specific dtype and byte ordering can be achieved with the ndarray astype method: >>> swapped_end_arr = big_end_arr.astype('<i2') >>> swapped_end_arr[0] 1 >>> swapped_end_arr.tobytes() == big_end_str False """
"""Audio Processor. Takes live audio and generates MIDI information from it. Here are the configuration options, and they go on and on. Some of them are optimistic that I'll come back later and add other options, but don't count on it. Probably the most important thing to wrap your head around is that you set a frame size for audio capture (say, 512), and each audio processor uses stores some multiple of that before it does anything. A multiple of "4" means the audio processor waits until 2048 bytes (4x512) have arrived before doing anything. The hop size (which aubio uses as a window for its work) is then a multiple of that. Most of the time it seems like the hop size should be exactly half of the window size, so you'd put .5 in that configuration option. If the window size and hop size should be the same (which worked best for me for beat detection), enter 1 as the configuration option. On the MIDI side, nearly everything is about what controller, note, or sysex message should be used to output information. Presumably you'll just need one or the other for each audio processor. It really depends on what is going to be on the receiving end of this information how you set this up. The defaults use some safe options--manufacturer is the MIDI "for educational use" prefix, and the controllers are not officially mapped to anything. Notes are only (potentially) used for pitches, but it should be clear how you could modify the code to use notes for anything else as well. Usage: audioprocessor.py [options] Options: -h --help Show this screen. Quits after. --listsounddevices List available sound devices. Quits after. --listmidiports List MIDI ports. Quits after. --writeinifile Write options to ini file, as specified by inifile option. If the file already present, a backup is made of original. --inifile=FILE Name of options settings file. [default: soundtomidi.ini] --inputdevice=DEVICE ID of the sound input device. System default audio input device will be used if not specified. [default: default] --channels=CHANNELS Number of channels to capture. [default: 1] --samplerate=SAMPLERATE Capture rate for audio samples. [default: 44100] --framesize=FRAMESIZE Size of each frame captured. [default: 512] --stdout=STDOUT Echo message to standard out. [default: False] --stdoutformat=STDOUTFORMAT Format for standard out messages. Options are "verbose", "bytes", "bin" or "hex". [default: verbose] --midiout=MIDIOUT Send MIDI messages? [default: True] --outport=MIDIOUTPORT Name of the MIDI output port. If left as default, uses first MIDI port found. [default: default] --outchannel=OUTCHANNEL Number of the MIDI channel to send messages on. Valid numbers 1-16. [default: 14] --sysexmanf=MANF Manufacturer prefix code for sysex messages. Int or hex values, separarated by space. [default: 0x7D] --gettempo=TEMPO Get the tempo of the audio. [default: True] --talg=TALG Aubio algorithm for determining the tempo. [default: default] --tframemult=TFRAMEMULT Number of frames to use in calculation. [default: 1] --thopmult=THOPMULT Hop size, as percent of FRAMEMULT. [default: .5] --taverage=TAVERAGE Number of BPM values to average. [default: 1] --tcount=TCOUNT Number of BPM averages to be stored before the most common one is sent as a message. [default: 1] --tcontrolnum=TCONTROLNUM Controller number to send BPM messages. If "None", no control messages will be sent. [default: 14] --tcontroltype=TCONTROLTYPE How to encode the BPM value for control. "minus60" sends BPM value minus 60. EG: 60 BPM = 0 value, 120 BPM = 60 value, 187 BPM = 127 value. [default: minus60] --tsysexnum=TSYSEXNUM Prefix to send prior to BPM in sysex messages. If "None", no sysex messages will be sent. [default: 0x0B] --tsysextype=TSYSEXTYPE How to encode the BPM value for sysex. "minus60" sends BPM value minus 60. (See --tsysexcontroltype) "twobytes" takes the BPM value to the tenth (EG, 128.1), multiplies it by 10 (1281), then spreads this across two 7 bit values (0x10 0x01) [default: twobytes] --getbeats=BEATS Get the beats of the audio. [default: True] --balg=BALG Aubio algorithm to use for the beat. [default: default] --bframemult=BFRAMEMULT Number of frames to use in calculation. [default: 1] --bhopmult=BHOPMULT Hop size, as percent of FRAMEMULT. [default: 1] --bcontrolnum=BCONTROLNUM Controller number to send beat messages. If "None", no control messages will be sent. [default: 15] --bsysexnum=BSYSEXNUM Prefix to send prior to beat number in sysex messages. If "None", no sysex messages will be sent. [default: 0x1B] --bvaltype=BVALTYPE Type of value to send with beat controller or sysex message. Any arbitrary number 0-127, or a comma separated looping listing of values to send. For example, "0,1,2,3" will send "0" for the first beat, "3" for fourth beat, then back to "0" for the next one. [default: 0,1,2,3,4,5,6,7] --bclock=BCLOCK Send 24 clock ticks messages after each beat. (Not yet implemented) [default: False] --getrms=RMS Get the RMS. [default: True] --rframemult=FFRAMEMULT Number of frames to use in calculation. [default: 4] --rhopmult=FHOPMULT Hop size, as percent of FRAMEMULT. [default: 1] --rcontrolnum=FRMSNUM Controller number to send RMS messages. If "None", no frequency strength sysex messages will be sent. [default: 20] --rsysexnum=FSYSEXNUM Prefix to send prior to RMS values If "None", no RMS sysex messages will be sent. [default: 0x1F] --rgraceful=FRMSGRACEFUL Gracefully let go of RMS peaks. EG: one frame peaks at 100, followed by a drop to 20. Instead of immediately reflecting the new value, this rule sets a cut-off for the drop to the chosen percent. The higher the percent, the slower the decline. New high peaks reset this graceful fade and it starts again. Set to 0.0 to turn off. [default: .5] --getfrequencies=FREQS Get the strength of filtered frequencies. [default: True] --falg=FALG Aubio algorithm to use for determining the strength of the frequencies. [default: default] --fframemult=FFRAMEMULT Number of frames to use in calculation. [default: 4] --fhopmult=FHOPMULT Hop size, as percent of FRAMEMULT. [default: 1] --fcount=FCOUNT Number of frequency values to hold before taking any action. Maximum value of set will be sent. [default: 2] --fbuckets=FBUCKETS Filter bands to use for use for dividing up frequencies. Comma separated list of values plus a low and high end barrier value. See Aubio docs "filterbanks" for more details. Shortcuts "octave" and "third-octave" shortcut for standard octave or 1/3 octave bands. [default: third-octave] --fsysexnum=FSYSEXNUM Prefix to send prior to frequency strength values. If "None", no sysex messages will be sent. [default: 0x0F] --fgraceful=FGRACEFUL Gracefully let go of frequency peaks. EG: a frame peaks at 100, followed by a drop to 20. Instead of immediately reflecting the new value, this rule sets a cut-off for the drop to the chosen percent. The higher the percent, the slower the decline. New high peaks reset this graceful fade and it starts again. Set to 0.0 to turn off. [default: .8] --getpitch=PITCHES Get the fundamental pitch of the audio. [default: True] --palg=PALG Aubio algorithm to use for pitch of the audio. [default: yin] --pframemult=PFRAMEMULT Number of frames to use in calculation. [default: 2] --phopmult=PHOPMULT Hop size, as percent of FRAMEMULT. [default: .5] --ptolerance=PTOLERANCE Required confidence level for a pitch. [default: 0.5] --pcount=PCOUNT Number of pitch averages to be stored before the most common one is sent as a message. [default: 8] --plowcutoff=PLOWCUTOFF Lowest pitch to consider. [default: 0] --phighcutoff=PHIGHCUTOFF Highest pitch to consider. [default: 127] --pfoldoctaves=PFOLDOCTAVES Return just 12 note values instead of the possible 128. [default: False] --pnumoffset=PNUMOFFSET Used only with the above option. Shifts the "C" value to somewhere else, and each note above that. Middle C is "60", which is the default. [default: 60] --pnoteon=PNOTEON Send note on messages for the audio pitch. [default: True] --pnoteoff=PNOTEOFF Send note off messages when a new audio pitch doesn't match previous pitch. [default: True] --pcontrolnum=PCONTROLNUM Controller number to send pitches. If "None", no control messages will be sent. [default: 21] --psysexnum=PSYSEXNUM Prefix to send prior to sending note value. If "None", no sysex messages will be sent. [default: 0x09] """
# -*- coding: utf-8 -*- # -- Dual Licence ---------------------------------------------------------- ############################################################################ # GPL License # # # # This file is a SCons (http://www.scons.org/) builder # # Copyright (c) 2012-14, NAME <EMAIL> # # This program is free software: you can redistribute it and/or modify # # it under the terms of the GNU General Public License as # # published by the Free Software Foundation, either version 3 of the # # License, or (at your option) any later version. # # # # This program is distributed in the hope that it will be useful, # # but WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # # GNU General Public License for more details. # # # # You should have received a copy of the GNU General Public License # # along with this program. If not, see <http://www.gnu.org/licenses/>. # ############################################################################ # -------------------------------------------------------------------------- ############################################################################ # BSD 3-Clause License # # # # This file is a SCons (http://www.scons.org/) builder # # Copyright (c) 2012-14, NAME <EMAIL> # # All rights reserved. # # # # Redistribution and use in source and binary forms, with or without # # modification, are permitted provided that the following conditions are # # met: # # # # 1. Redistributions of source code must retain the above copyright # # notice, this list of conditions and the following disclaimer. # # # # 2. Redistributions in binary form must reproduce the above copyright # # notice, this list of conditions and the following disclaimer in the # # documentation and/or other materials provided with the distribution. # # # # 3. Neither the name of the copyright holder nor the names of its # # contributors may be used to endorse or promote products derived from # # this software without specific prior written permission. # # # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A # # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT # # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED # # TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR # # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF # # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING # # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ############################################################################ # The Unpack Builder can be used for unpacking archives (eg Zip, TGZ, BZ, ... ). # The emitter of the Builder reads the archive data and creates a returning file list # the builder extract the archive. The environment variable stores a dictionary "UNPACK" # for set different extractions (subdict "EXTRACTOR"): # { # PRIORITY => a value for setting the extractor order (lower numbers = extractor is used earlier) # SUFFIX => defines a list with file suffixes, which should be handled with this extractor # EXTRACTSUFFIX => suffix of the extract command # EXTRACTFLAGS => a string parameter for the RUN command for extracting the data # EXTRACTCMD => full extract command of the builder # RUN => the main program which will be started (if the parameter is empty, the extractor will be ignored) # LISTCMD => the listing command for the emitter # LISTFLAGS => the string options for the RUN command for showing a list of files # LISTSUFFIX => suffix of the list command # LISTEXTRACTOR => a optional Python function, that is called on each output line of the # LISTCMD for extracting file & dir names, the function need two parameters (first line number, # second line content) and must return a string with the file / dir path (other value types # will be ignored) # } # Other options in the UNPACK dictionary are: # STOPONEMPTYFILE => bool variable for stoping if the file has empty size (default True) # VIWEXTRACTOUTPUT => shows the output messages of the extraction command (default False) # EXTRACTDIR => path in that the data will be extracted (default #) # # The file which is handled by the first suffix match of the extractor, the extractor list can be append for other files. # The order of the extractor dictionary creates the listing & extractor command eg file extension .tar.gz should be # before .gz, because the tar.gz is extract in one shoot. # # Under *nix system these tools are supported: tar, bzip2, gzip, unzip # Under Windows only 7-Zip (http://www.7-zip.org/) is supported
""" Matplotlib provides sophisticated date plotting capabilities, standing on the shoulders of python :mod:`datetime`, the add-on modules :mod:`pytz` and :mod:`dateutil`. :class:`datetime` objects are converted to floating point numbers which represent time in days since 0001-01-01 UTC, plus 1. For example, 0001-01-01, 06:00 is 1.25, not 0.25. The helper functions :func:`date2num`, :func:`num2date` and :func:`drange` are used to facilitate easy conversion to and from :mod:`datetime` and numeric ranges. .. note:: Like Python's datetime, mpl uses the Gregorian calendar for all conversions between dates and floating point numbers. This practice is not universal, and calendar differences can cause confusing differences between what Python and mpl give as the number of days since 0001-01-01 and what other software and databases yield. For example, the US Naval Observatory uses a calendar that switches from Julian to Gregorian in October, 1582. Hence, using their calculator, the number of days between 0001-01-01 and 2006-04-01 is 732403, whereas using the Gregorian calendar via the datetime module we find:: In [31]:date(2006,4,1).toordinal() - date(1,1,1).toordinal() Out[31]:732401 A wide range of specific and general purpose date tick locators and formatters are provided in this module. See :mod:`matplotlib.ticker` for general information on tick locators and formatters. These are described below. All the matplotlib date converters, tickers and formatters are timezone aware, and the default timezone is given by the timezone parameter in your :file:`matplotlibrc` file. If you leave out a :class:`tz` timezone instance, the default from your rc file will be assumed. If you want to use a custom time zone, pass a :class:`pytz.timezone` instance with the tz keyword argument to :func:`num2date`, :func:`plot_date`, and any custom date tickers or locators you create. See `pytz <http://pythonhosted.org/pytz/>`_ for information on :mod:`pytz` and timezone handling. The `dateutil module <https://dateutil.readthedocs.io/en/stable/>`_ provides additional code to handle date ticking, making it easy to place ticks on any kinds of dates. See examples below. Date tickers ------------ Most of the date tickers can locate single or multiple values. For example:: # import constants for the days of the week from matplotlib.dates import MO, TU, WE, TH, FR, SA, SU # tick on mondays every week loc = WeekdayLocator(byweekday=MO, tz=tz) # tick on mondays and saturdays loc = WeekdayLocator(byweekday=(MO, SA)) In addition, most of the constructors take an interval argument:: # tick on mondays every second week loc = WeekdayLocator(byweekday=MO, interval=2) The rrule locator allows completely general date ticking:: # tick every 5th easter rule = rrulewrapper(YEARLY, byeaster=1, interval=5) loc = RRuleLocator(rule) Here are all the date tickers: * :class:`MinuteLocator`: locate minutes * :class:`HourLocator`: locate hours * :class:`DayLocator`: locate specifed days of the month * :class:`WeekdayLocator`: Locate days of the week, e.g., MO, TU * :class:`MonthLocator`: locate months, e.g., 7 for july * :class:`YearLocator`: locate years that are multiples of base * :class:`RRuleLocator`: locate using a :class:`matplotlib.dates.rrulewrapper`. The :class:`rrulewrapper` is a simple wrapper around a :class:`dateutil.rrule` (`dateutil <https://dateutil.readthedocs.io/en/stable/>`_) which allow almost arbitrary date tick specifications. See `rrule example <../examples/pylab_examples/date_demo_rrule.html>`_. * :class:`AutoDateLocator`: On autoscale, this class picks the best :class:`MultipleDateLocator` to set the view limits and the tick locations. Date formatters --------------- Here all all the date formatters: * :class:`AutoDateFormatter`: attempts to figure out the best format to use. This is most useful when used with the :class:`AutoDateLocator`. * :class:`DateFormatter`: use :func:`strftime` format strings * :class:`IndexDateFormatter`: date plots with implicit *x* indexing. """
""" Filters ------- Filters are an interesting and somewhat challenging part of the code base. They are used for two different purposes: - To figure out which nodes in the xml hierarchy to start rendering from. These are called 'finder filters' or 'content filters'. This is done before rendering starts. - To figure out which nodes under a selected nodes in the xml hierarchy should be rendered. These are called 'render filters'. This is done during the render process with a test in the DoxygenToRstRendererFactory. General Implementation ~~~~~~~~~~~~~~~~~~~~~~ Filters are essential just tests to see if a node matches certain parameters that are needed to decide whether or not to include it in some output. As these filters are declared once and then used on multiple nodes, we model them as object hierarchies that encapsulate the required test and take a node (with its context) and return True or False. If you wanted a test which figures out if a node has the node_type 'memberdef' you might create the following object hierarchy: node_is_memberdef = InFilter(AttributeAccessor(Node(), 'node_type'), ['memberdef']) This reads from the inside out, as get the node, then get the node_type attribute from it, and see if the value of the attribute is in the list ['memberdef']. The Node() is called a 'Selector'. Parent() is also a selector. It means given the current context, work with the parent of the current node rather than the node itself. This allows you to frame tests in terms of a node's parent as well as the node which helps when we want nodes with particular parents and not others. The AttributeAccessor() is called an 'Accessor'. It wraps up an attempt to access a particular attribute on the selected node. There are quite a few different specific accessors but they can mostly be generalised with the AttributeAccessor. This code has evolved over time and initially the implementation involved specific accessor classes (which are still used in large parts of it.) The InFilter() is unsurprisingly called a 'Filter'. There are lots of different filters. Filters either act on the results of Accessors or on the results of other Filters and they always return True or False. The AndFilter and the OrFilter can be used to combine the outputs of other Filters with logical 'and' and 'or' operations. You can build up some pretty complex expressions with this level of freedom as you might imagine. The complexity is unfortunate but necessary as the nature of filtering the xml is quite complex. Finder Filters ~~~~~~~~~~~~~~ The implementation of the filters can change a little depending on how they are called. Finder filters are called from the breathe.finder.doxygen.index and breathe.finder.doxygen.compound files. They are called like this: # Descend down the hierarchy # ... if filter_.allow(node_stack): matches.append(self.data_object) # Keep on descending # ... This means that the result of the filter does not stop us descending down the hierarchy and testing more nodes. This simplifies the filters as they only have to return true for the exact nodes they are interested in and they don't have to worry about allowing the iteration down the hierarchy to continue for nodes which don't match. An example of a finder filter is: AndFilter( InFilter(NodeTypeAccessor(Node()), ["compound"]), InFilter(KindAccessor(Node()), ["group"]), InFilter(NameAccessor(Node()), ["mygroup"]) ) This says, return True for all the nodes of node_type 'compound' with 'kind' set to 'group' which have the name 'mygroup'. It returns false for everything else, but when a node matching this is found then it is added to the matches list by the code above. It is therefore relatively easy to write finder filters. If you have two separate node filters like the one above and you want to match on both of them then you can do: OrFilter( node_filter_1, node_filter_2 ) To combine them. Content Filters ~~~~~~~~~~~~~~~ Content filters are harder than the finder filters as they are responsible for halting the iteration down the hierarchy if they return false. This means that if you're interested in memberdef nodes with a particular attribute then you have to check for that but also include a clause which allows all other non-memberdef nodes to pass through as you don't want to interrupt them. This means you end up with filters like this: OrFilter( AndFilter( InFilter(NodeTypeAccessor(Node()), ["compound"]), InFilter(KindAccessor(Node()), ["group"]), InFilter(NameAccessor(Node()), ["mygroup"]) ), NotFilter( AndFilter( InFilter(NodeTypeAccessor(Node()), ["compound"]), InFilter(KindAccessor(Node()), ["group"]), ) ) ) Which is to say that we want to let through a compound, with kind group, with name 'mygroup' but we're also happy if the node is **not** a compund with kind group. Really we just don't want to let through any compounds with kind group with name other than 'mygroup'. As such, we can rephrase this as: NotFilter( AndFilter( InFilter(NodeTypeAccessor(Node()), ["compound"]), InFilter(KindAccessor(Node()), ["group"]), NotFilter(InFilter(NameAccessor(Node()), ["mygroup"])) ) ) Using logical manipulation we can rewrite this as: OrFilter( NotFilter(InFilter(NodeTypeAccessor(Node()), ["compound"])), NotFilter(InFilter(KindAccessor(Node()), ["group"])), InFilter(NameAccessor(Node()), ["mygroup"]) ) We reads: allow if it isn't a compound, or if it is a compound but doesn't have a 'kind' of 'group', but if it is a compound and has a 'kind' of 'group then only allow it if it is named 'mygroup'. Helper Syntax ~~~~~~~~~~~~~ Some of these filter declarations get a little awkward to read and write. They are not laid out in manner which reads smoothly. Additional helper methods and operator overloads have been introduced to help with this. AttributeAccessor objects are created in property methods on the Selector classes so: node.kind Where node has been declared as a Node() instance. Results in: AttributeAccessor(Node(), 'kind') The '==' and '!=' operators on the Accessors have been overloaded to return the appropriate filters so that: node.kind == 'group' Results in: InFilter(AttributeAccessor(Node(), 'kind'), ['kind']) We also override the binary 'and' (&), 'or' (|) and 'not' (~) operators in Python to apply AndFilters, OrFilters and NotFilters respectively. We have to override the binary operators as they actual 'and', 'or' and 'not' operators cannot be overridden. So: (node.node_type == 'compound') & (node.name == 'mygroup') Translates to: AndFilter( InFilter(NodeTypeAccessor(Node()), ["compound"])), InFilter(NameAccessor(Node()), ["mygroup"]) ) Where the former is hopefully more readable without sacrificing too much to the abstract magic of operator overloads. Operator Precedences & Extra Parenthesis '''''''''''''''''''''''''''''''''''''''' As the binary operators have a lower operator precedence than '==' and '!=' and some other operators we have to include additional parenthesis in the expressions to group them as we want. So instead of writing: node.node_type == 'compound' & node.name == 'mygroup' We have to write: (node.node_type == 'compound') & (node.name == 'mygroup') """
""" ============ Array basics ============ Array types and conversions between types ========================================= NumPy supports a much greater variety of numerical types than Python does. This section shows which are available, and how to modify an array's data-type. ========== ========================================================== Data type Description ========== ========================================================== bool_ Boolean (True or False) stored as a byte int_ Default integer type (same as C ``long``; normally either ``int64`` or ``int32``) intc Identical to C ``int`` (normally ``int32`` or ``int64``) intp Integer used for indexing (same as C ``ssize_t``; normally either ``int32`` or ``int64``) int8 Byte (-128 to 127) int16 Integer (-32768 to 32767) int32 Integer (-2147483648 to 2147483647) int64 Integer (-9223372036854775808 to 9223372036854775807) uint8 Unsigned integer (0 to 255) uint16 Unsigned integer (0 to 65535) uint32 Unsigned integer (0 to 4294967295) uint64 Unsigned integer (0 to 18446744073709551615) float_ Shorthand for ``float64``. float16 Half precision float: sign bit, 5 bits exponent, 10 bits mantissa float32 Single precision float: sign bit, 8 bits exponent, 23 bits mantissa float64 Double precision float: sign bit, 11 bits exponent, 52 bits mantissa complex_ Shorthand for ``complex128``. complex64 Complex number, represented by two 32-bit floats (real and imaginary components) complex128 Complex number, represented by two 64-bit floats (real and imaginary components) ========== ========================================================== Additionally to ``intc`` the platform dependent C integer types ``short``, ``long``, ``longlong`` and their unsigned versions are defined. NumPy numerical types are instances of ``dtype`` (data-type) objects, each having unique characteristics. Once you have imported NumPy using :: >>> import numpy as np the dtypes are available as ``np.bool_``, ``np.float32``, etc. Advanced types, not listed in the table above, are explored in section :ref:`structured_arrays`. There are 5 basic numerical types representing booleans (bool), integers (int), unsigned integers (uint) floating point (float) and complex. Those with numbers in their name indicate the bitsize of the type (i.e. how many bits are needed to represent a single value in memory). Some types, such as ``int`` and ``intp``, have differing bitsizes, dependent on the platforms (e.g. 32-bit vs. 64-bit machines). This should be taken into account when interfacing with low-level code (such as C or Fortran) where the raw memory is addressed. Data-types can be used as functions to convert python numbers to array scalars (see the array scalar section for an explanation), python sequences of numbers to arrays of that type, or as arguments to the dtype keyword that many numpy functions or methods accept. Some examples:: >>> import numpy as np >>> x = np.float32(1.0) >>> x 1.0 >>> y = np.int_([1,2,4]) >>> y array([1, 2, 4]) >>> z = np.arange(3, dtype=np.uint8) >>> z array([0, 1, 2], dtype=uint8) Array types can also be referred to by character codes, mostly to retain backward compatibility with older packages such as Numeric. Some documentation may still refer to these, for example:: >>> np.array([1, 2, 3], dtype='f') array([ 1., 2., 3.], dtype=float32) We recommend using dtype objects instead. To convert the type of an array, use the .astype() method (preferred) or the type itself as a function. For example: :: >>> z.astype(float) #doctest: +NORMALIZE_WHITESPACE array([ 0., 1., 2.]) >>> np.int8(z) array([0, 1, 2], dtype=int8) Note that, above, we use the *Python* float object as a dtype. NumPy knows that ``int`` refers to ``np.int_``, ``bool`` means ``np.bool_``, that ``float`` is ``np.float_`` and ``complex`` is ``np.complex_``. The other data-types do not have Python equivalents. To determine the type of an array, look at the dtype attribute:: >>> z.dtype dtype('uint8') dtype objects also contain information about the type, such as its bit-width and its byte-order. The data type can also be used indirectly to query properties of the type, such as whether it is an integer:: >>> d = np.dtype(int) >>> d dtype('int32') >>> np.issubdtype(d, int) True >>> np.issubdtype(d, float) False Array Scalars ============= NumPy generally returns elements of arrays as array scalars (a scalar with an associated dtype). Array scalars differ from Python scalars, but for the most part they can be used interchangeably (the primary exception is for versions of Python older than v2.x, where integer array scalars cannot act as indices for lists and tuples). There are some exceptions, such as when code requires very specific attributes of a scalar or when it checks specifically whether a value is a Python scalar. Generally, problems are easily fixed by explicitly converting array scalars to Python scalars, using the corresponding Python type function (e.g., ``int``, ``float``, ``complex``, ``str``, ``unicode``). The primary advantage of using array scalars is that they preserve the array type (Python may not have a matching scalar type available, e.g. ``int16``). Therefore, the use of array scalars ensures identical behaviour between arrays and scalars, irrespective of whether the value is inside an array or not. NumPy scalars also have many of the same methods arrays do. Extended Precision ================== Python's floating-point numbers are usually 64-bit floating-point numbers, nearly equivalent to ``np.float64``. In some unusual situations it may be useful to use floating-point numbers with more precision. Whether this is possible in numpy depends on the hardware and on the development environment: specifically, x86 machines provide hardware floating-point with 80-bit precision, and while most C compilers provide this as their ``long double`` type, MSVC (standard for Windows builds) makes ``long double`` identical to ``double`` (64 bits). NumPy makes the compiler's ``long double`` available as ``np.longdouble`` (and ``np.clongdouble`` for the complex numbers). You can find out what your numpy provides with``np.finfo(np.longdouble)``. NumPy does not provide a dtype with more precision than C ``long double``s; in particular, the 128-bit IEEE quad precision data type (FORTRAN's ``REAL*16``) is not available. For efficient memory alignment, ``np.longdouble`` is usually stored padded with zero bits, either to 96 or 128 bits. Which is more efficient depends on hardware and development environment; typically on 32-bit systems they are padded to 96 bits, while on 64-bit systems they are typically padded to 128 bits. ``np.longdouble`` is padded to the system default; ``np.float96`` and ``np.float128`` are provided for users who want specific padding. In spite of the names, ``np.float96`` and ``np.float128`` provide only as much precision as ``np.longdouble``, that is, 80 bits on most x86 machines and 64 bits in standard Windows builds. Be warned that even if ``np.longdouble`` offers more precision than python ``float``, it is easy to lose that extra precision, since python often forces values to pass through ``float``. For example, the ``%`` formatting operator requires its arguments to be converted to standard python types, and it is therefore impossible to preserve extended precision even if many decimal places are requested. It can be useful to test your code with the value ``1 + np.finfo(np.longdouble).eps``. """
# Copyright 2017 NAME - t-h-i-n-x.net # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # Changelog # # 1.0 2017/01/03 Initial release # # Config parameters # # - slave 8 bit Value of the I2C slave address for the chip. # Defaults to 0x40. Possible values are from 0x40 to 0x4F. # - shunt Float Value of the shunt resistor in Ohms. Default is 0.1. # - vrange Integer Vrange value of the chip. Valid values are 16 or 32. # Default is 32. # - gaindiv Integer Gain divider (PGA) value of the chip. Valid values # are from (1, 2, 4 , 8). Default is 8. # - mode Integer Value of the chip mode. Possible values are from # 0x0 to 0x7. Default is 0x7. # - badc Integer Value of the voltage bus ADC settings. Possible # values are from 0x0 to 0xF. Default is 0x3. # - sadc Integer Value of the shunt voltage ADC settings. Possible # values are from 0x0 to 0xF. Default is 0x3. # - vmax Float Value of the desired vmax value for automatic # calibration. Default is None. This parameter will # only be used of imax is also not None. # - imax Float Value of the desired imax value for automatic # calibration. Default is None. If imax is given, # the values for vrange, gaindiv and currentLSB will be # ignored and calculated instead. If imax is higher than # possible, then the highest possible value will be # used instead and overflow may occur. # - currentLSB Float Value of the current LSB to use. Default is None. # If you mistrust the automatic calibration you can # set the current LSB manual with this parameter. If # used, make sure to manual set the desired gaindiv also. # - bus String Name of the I2C bus # # Usage remarks # # - The default values of this driver are valid for a 32 V Bus range, a maximum # possible current of 3.2 A and a current resolution of around 98 microAmperes/Bit. # If you are fine with this you can just use those defaults. # - If you want to have some more configuration while keeping it still simple you # can provide parameters for vmax and imax and the driver will do its best to # automatically calculate vrange, gaindiv and calibration with a very good resolution. # - If you prefer complete manual setup you should set vrange, gaindiv, currentLSB and # optional fine-tuned calibration (in this order). # - Setting the calibration register via setCalibration() is to be used for the final # calibration as explained in the chip spec for the final fine tuning. It must not # be used for the currentLSB setting as this is calculated automatically by this # driver based on the values of shunt and gaindiv. # - This driver implements an automatical calibration feature calibrate(vmax, imax) # that can be used during device creation and also at runtime. The value for vmax # is used to set vrange within the allowed limits. The value for imax is used to # set gaindiv so that the maximal desired current can be measured at the highest # possible resolution for current LSB. If the desired imax is higher than the # possible imax based on the value of shunt, then the maximum possible imax will # be used. You get the choosen values via the response of the calibrate(...) call. # In this case, sending a higher current through the shunt will result in overflow # which will generate a debugging message (only when reading the bus voltage). # - If values for vmax and imax are given at device creation they will override the # init values for vrange and gaindiv as those will be ignored then and calculated via # the automatic calibration feature instead. # - All chip parameters with the exception of shunt can be changed at runtime. If # an updated parameter has an influence on the currentLSB and/or calibration value, # then this/these will be re-calculated automatically and the calibration register # will be set also. If you use setCalibration() for final fine-tuning you have to # repeat that step again if automatic calibration has taken place. # - Updating of the mode value at runtime allows triggered conversions and power-down # of the chip. # - If you are unsure about the calculated values set debugging to "True" and look at # the debugging messages as they will notify you about all resulting values. Or # call getConfiguration() to see all values. # - If you encounter overflow (getting the overflow error) try to increase the # gaindiv value or reduce the shunt value (please as real hardware change). # # Implementation remarks # # - This driver is implemented based on the specs from Intel. # - The default value for the shunt resistor of 0.1 Ohms is appropriate for the # breakout board from Adafruit for this chip (Adafruit PRODUCT ID: 904). # - The parameter value for shunt can't be changed at runtime after device # creation because it is very unlikely to modify the shunt resistor during operation # of the chip. Please provide the correct value via the config options or at # device creation if the default value does not suit your hardware setup. # - This driver uses floating point calculation and takes no care about integer # only arithmetics. For that reason, the mathematical lowest possible LSB value is # calculated automatically and used for best resolution with the exception when you # manual set your own current LSB value. # - If you want to override/select the current LSB value manual you can do that # via config parameter or at runtime. In this case make sure to use the correct # corresponding gaindiv value otherwise the value readings will be wrong. # - If for some reason (e.g. an impropriate setting of the currentLSB) the value # of the calibration register would be out of its allowed bounds it will be set # to zero so that all current and power readings will also be zero to avoid wrong # measurements until the calibration register is set again to an allowed range. # - This driver does not use the shunt adc register as this value is not needed # for operation if the calibration register is used. #
""" ======================================== Special functions (:mod:`scipy.special`) ======================================== .. module:: scipy.special Nearly all of the functions below are universal functions and follow broadcasting and automatic array-looping rules. Exceptions are noted. .. seealso:: `scipy.special.cython_special` -- Typed Cython versions of special functions Error handling ============== Errors are handled by returning NaNs or other appropriate values. Some of the special function routines can emit warnings when an error occurs. By default this is disabled; to enable it use `errprint`. .. autosummary:: :toctree: generated/ errprint -- Set or return the error printing flag for special functions. SpecialFunctionWarning -- Warning that can be issued with ``errprint(True)`` Available functions =================== Airy functions -------------- .. autosummary:: :toctree: generated/ airy -- Airy functions and their derivatives. airye -- Exponentially scaled Airy functions and their derivatives. ai_zeros -- [+]Compute `nt` zeros and values of the Airy function Ai and its derivative. bi_zeros -- [+]Compute `nt` zeros and values of the Airy function Bi and its derivative. itairy -- Integrals of Airy functions Elliptic Functions and Integrals -------------------------------- .. autosummary:: :toctree: generated/ ellipj -- Jacobian elliptic functions ellipk -- Complete elliptic integral of the first kind. ellipkm1 -- Complete elliptic integral of the first kind around `m` = 1 ellipkinc -- Incomplete elliptic integral of the first kind ellipe -- Complete elliptic integral of the second kind ellipeinc -- Incomplete elliptic integral of the second kind Bessel Functions ---------------- .. autosummary:: :toctree: generated/ jv -- Bessel function of the first kind of real order and complex argument. jn -- Bessel function of the first kind of real order and complex argument jve -- Exponentially scaled Bessel function of order `v`. yn -- Bessel function of the second kind of integer order and real argument. yv -- Bessel function of the second kind of real order and complex argument. yve -- Exponentially scaled Bessel function of the second kind of real order. kn -- Modified Bessel function of the second kind of integer order `n` kv -- Modified Bessel function of the second kind of real order `v` kve -- Exponentially scaled modified Bessel function of the second kind. iv -- Modified Bessel function of the first kind of real order. ive -- Exponentially scaled modified Bessel function of the first kind hankel1 -- Hankel function of the first kind hankel1e -- Exponentially scaled Hankel function of the first kind hankel2 -- Hankel function of the second kind hankel2e -- Exponentially scaled Hankel function of the second kind The following is not an universal function: .. autosummary:: :toctree: generated/ lmbda -- [+]Jahnke-Emden Lambda function, Lambdav(x). Zeros of Bessel Functions ^^^^^^^^^^^^^^^^^^^^^^^^^ These are not universal functions: .. autosummary:: :toctree: generated/ jnjnp_zeros -- [+]Compute zeros of integer-order Bessel functions Jn and Jn'. jnyn_zeros -- [+]Compute nt zeros of Bessel functions Jn(x), Jn'(x), Yn(x), and Yn'(x). jn_zeros -- [+]Compute zeros of integer-order Bessel function Jn(x). jnp_zeros -- [+]Compute zeros of integer-order Bessel function derivative Jn'(x). yn_zeros -- [+]Compute zeros of integer-order Bessel function Yn(x). ynp_zeros -- [+]Compute zeros of integer-order Bessel function derivative Yn'(x). y0_zeros -- [+]Compute nt zeros of Bessel function Y0(z), and derivative at each zero. y1_zeros -- [+]Compute nt zeros of Bessel function Y1(z), and derivative at each zero. y1p_zeros -- [+]Compute nt zeros of Bessel derivative Y1'(z), and value at each zero. Faster versions of common Bessel Functions ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. autosummary:: :toctree: generated/ j0 -- Bessel function of the first kind of order 0. j1 -- Bessel function of the first kind of order 1. y0 -- Bessel function of the second kind of order 0. y1 -- Bessel function of the second kind of order 1. i0 -- Modified Bessel function of order 0. i0e -- Exponentially scaled modified Bessel function of order 0. i1 -- Modified Bessel function of order 1. i1e -- Exponentially scaled modified Bessel function of order 1. k0 -- Modified Bessel function of the second kind of order 0, :math:`K_0`. k0e -- Exponentially scaled modified Bessel function K of order 0 k1 -- Modified Bessel function of the second kind of order 1, :math:`K_1(x)`. k1e -- Exponentially scaled modified Bessel function K of order 1 Integrals of Bessel Functions ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. autosummary:: :toctree: generated/ itj0y0 -- Integrals of Bessel functions of order 0 it2j0y0 -- Integrals related to Bessel functions of order 0 iti0k0 -- Integrals of modified Bessel functions of order 0 it2i0k0 -- Integrals related to modified Bessel functions of order 0 besselpoly -- [+]Weighted integral of a Bessel function. Derivatives of Bessel Functions ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. autosummary:: :toctree: generated/ jvp -- Compute nth derivative of Bessel function Jv(z) with respect to `z`. yvp -- Compute nth derivative of Bessel function Yv(z) with respect to `z`. kvp -- Compute nth derivative of real-order modified Bessel function Kv(z) ivp -- Compute nth derivative of modified Bessel function Iv(z) with respect to `z`. h1vp -- Compute nth derivative of Hankel function H1v(z) with respect to `z`. h2vp -- Compute nth derivative of Hankel function H2v(z) with respect to `z`. Spherical Bessel Functions ^^^^^^^^^^^^^^^^^^^^^^^^^^ .. autosummary:: :toctree: generated/ spherical_jn -- Spherical Bessel function of the first kind or its derivative. spherical_yn -- Spherical Bessel function of the second kind or its derivative. spherical_in -- Modified spherical Bessel function of the first kind or its derivative. spherical_kn -- Modified spherical Bessel function of the second kind or its derivative. Riccati-Bessel Functions ^^^^^^^^^^^^^^^^^^^^^^^^ These are not universal functions: .. autosummary:: :toctree: generated/ riccati_jn -- [+]Compute Ricatti-Bessel function of the first kind and its derivative. riccati_yn -- [+]Compute Ricatti-Bessel function of the second kind and its derivative. Struve Functions ---------------- .. autosummary:: :toctree: generated/ struve -- Struve function. modstruve -- Modified Struve function. itstruve0 -- Integral of the Struve function of order 0. it2struve0 -- Integral related to the Struve function of order 0. itmodstruve0 -- Integral of the modified Struve function of order 0. Raw Statistical Functions ------------------------- .. seealso:: :mod:`scipy.stats`: Friendly versions of these functions. .. autosummary:: :toctree: generated/ bdtr -- Binomial distribution cumulative distribution function. bdtrc -- Binomial distribution survival function. bdtri -- Inverse function to `bdtr` with respect to `p`. bdtrik -- Inverse function to `bdtr` with respect to `k`. bdtrin -- Inverse function to `bdtr` with respect to `n`. btdtr -- Cumulative density function of the beta distribution. btdtri -- The `p`-th quantile of the beta distribution. btdtria -- Inverse of `btdtr` with respect to `a`. btdtrib -- btdtria(a, p, x) fdtr -- F cumulative distribution function. fdtrc -- F survival function. fdtri -- The `p`-th quantile of the F-distribution. fdtridfd -- Inverse to `fdtr` vs dfd gdtr -- Gamma distribution cumulative density function. gdtrc -- Gamma distribution survival function. gdtria -- Inverse of `gdtr` vs a. gdtrib -- Inverse of `gdtr` vs b. gdtrix -- Inverse of `gdtr` vs x. nbdtr -- Negative binomial cumulative distribution function. nbdtrc -- Negative binomial survival function. nbdtri -- Inverse of `nbdtr` vs `p`. nbdtrik -- Inverse of `nbdtr` vs `k`. nbdtrin -- Inverse of `nbdtr` vs `n`. ncfdtr -- Cumulative distribution function of the non-central F distribution. ncfdtridfd -- Calculate degrees of freedom (denominator) for the noncentral F-distribution. ncfdtridfn -- Calculate degrees of freedom (numerator) for the noncentral F-distribution. ncfdtri -- Inverse cumulative distribution function of the non-central F distribution. ncfdtrinc -- Calculate non-centrality parameter for non-central F distribution. nctdtr -- Cumulative distribution function of the non-central `t` distribution. nctdtridf -- Calculate degrees of freedom for non-central t distribution. nctdtrit -- Inverse cumulative distribution function of the non-central t distribution. nctdtrinc -- Calculate non-centrality parameter for non-central t distribution. nrdtrimn -- Calculate mean of normal distribution given other params. nrdtrisd -- Calculate standard deviation of normal distribution given other params. pdtr -- Poisson cumulative distribution function pdtrc -- Poisson survival function pdtri -- Inverse to `pdtr` vs m pdtrik -- Inverse to `pdtr` vs k stdtr -- Student t distribution cumulative density function stdtridf -- Inverse of `stdtr` vs df stdtrit -- Inverse of `stdtr` vs `t` chdtr -- Chi square cumulative distribution function chdtrc -- Chi square survival function chdtri -- Inverse to `chdtrc` chdtriv -- Inverse to `chdtr` vs `v` ndtr -- Gaussian cumulative distribution function. log_ndtr -- Logarithm of Gaussian cumulative distribution function. ndtri -- Inverse of `ndtr` vs x chndtr -- Non-central chi square cumulative distribution function chndtridf -- Inverse to `chndtr` vs `df` chndtrinc -- Inverse to `chndtr` vs `nc` chndtrix -- Inverse to `chndtr` vs `x` smirnov -- Kolmogorov-Smirnov complementary cumulative distribution function smirnovi -- Inverse to `smirnov` kolmogorov -- Complementary cumulative distribution function of Kolmogorov distribution kolmogi -- Inverse function to kolmogorov tklmbda -- Tukey-Lambda cumulative distribution function logit -- Logit ufunc for ndarrays. expit -- Expit ufunc for ndarrays. boxcox -- Compute the Box-Cox transformation. boxcox1p -- Compute the Box-Cox transformation of 1 + `x`. inv_boxcox -- Compute the inverse of the Box-Cox transformation. inv_boxcox1p -- Compute the inverse of the Box-Cox transformation. Information Theory Functions ---------------------------- .. autosummary:: :toctree: generated/ entr -- Elementwise function for computing entropy. rel_entr -- Elementwise function for computing relative entropy. kl_div -- Elementwise function for computing Kullback-Leibler divergence. huber -- Huber loss function. pseudo_huber -- Pseudo-Huber loss function. Gamma and Related Functions --------------------------- .. autosummary:: :toctree: generated/ gamma -- Gamma function. gammaln -- Logarithm of the absolute value of the Gamma function for real inputs. loggamma -- Principal branch of the logarithm of the Gamma function. gammasgn -- Sign of the gamma function. gammainc -- Regularized lower incomplete gamma function. gammaincinv -- Inverse to `gammainc` gammaincc -- Regularized upper incomplete gamma function. gammainccinv -- Inverse to `gammaincc` beta -- Beta function. betaln -- Natural logarithm of absolute value of beta function. betainc -- Incomplete beta integral. betaincinv -- Inverse function to beta integral. psi -- The digamma function. rgamma -- Gamma function inverted polygamma -- Polygamma function n. multigammaln -- Returns the log of multivariate gamma, also sometimes called the generalized gamma. digamma -- psi(x[, out]) poch -- Rising factorial (z)_m Error Function and Fresnel Integrals ------------------------------------ .. autosummary:: :toctree: generated/ erf -- Returns the error function of complex argument. erfc -- Complementary error function, ``1 - erf(x)``. erfcx -- Scaled complementary error function, ``exp(x**2) * erfc(x)``. erfi -- Imaginary error function, ``-i erf(i z)``. erfinv -- Inverse function for erf. erfcinv -- Inverse function for erfc. wofz -- Faddeeva function dawsn -- Dawson's integral. fresnel -- Fresnel sin and cos integrals fresnel_zeros -- Compute nt complex zeros of sine and cosine Fresnel integrals S(z) and C(z). modfresnelp -- Modified Fresnel positive integrals modfresnelm -- Modified Fresnel negative integrals These are not universal functions: .. autosummary:: :toctree: generated/ erf_zeros -- [+]Compute nt complex zeros of error function erf(z). fresnelc_zeros -- [+]Compute nt complex zeros of cosine Fresnel integral C(z). fresnels_zeros -- [+]Compute nt complex zeros of sine Fresnel integral S(z). Legendre Functions ------------------ .. autosummary:: :toctree: generated/ lpmv -- Associated Legendre function of integer order and real degree. sph_harm -- Compute spherical harmonics. These are not universal functions: .. autosummary:: :toctree: generated/ clpmn -- [+]Associated Legendre function of the first kind for complex arguments. lpn -- [+]Legendre function of the first kind. lqn -- [+]Legendre function of the second kind. lpmn -- [+]Sequence of associated Legendre functions of the first kind. lqmn -- [+]Sequence of associated Legendre functions of the second kind. Ellipsoidal Harmonics --------------------- .. autosummary:: :toctree: generated/ ellip_harm -- Ellipsoidal harmonic functions E^p_n(l) ellip_harm_2 -- Ellipsoidal harmonic functions F^p_n(l) ellip_normal -- Ellipsoidal harmonic normalization constants gamma^p_n Orthogonal polynomials ---------------------- The following functions evaluate values of orthogonal polynomials: .. autosummary:: :toctree: generated/ assoc_laguerre -- Compute the generalized (associated) Laguerre polynomial of degree n and order k. eval_legendre -- Evaluate Legendre polynomial at a point. eval_chebyt -- Evaluate Chebyshev polynomial of the first kind at a point. eval_chebyu -- Evaluate Chebyshev polynomial of the second kind at a point. eval_chebyc -- Evaluate Chebyshev polynomial of the first kind on [-2, 2] at a point. eval_chebys -- Evaluate Chebyshev polynomial of the second kind on [-2, 2] at a point. eval_jacobi -- Evaluate Jacobi polynomial at a point. eval_laguerre -- Evaluate Laguerre polynomial at a point. eval_genlaguerre -- Evaluate generalized Laguerre polynomial at a point. eval_hermite -- Evaluate physicist's Hermite polynomial at a point. eval_hermitenorm -- Evaluate probabilist's (normalized) Hermite polynomial at a point. eval_gegenbauer -- Evaluate Gegenbauer polynomial at a point. eval_sh_legendre -- Evaluate shifted Legendre polynomial at a point. eval_sh_chebyt -- Evaluate shifted Chebyshev polynomial of the first kind at a point. eval_sh_chebyu -- Evaluate shifted Chebyshev polynomial of the second kind at a point. eval_sh_jacobi -- Evaluate shifted Jacobi polynomial at a point. The following functions compute roots and quadrature weights for orthogonal polynomials: .. autosummary:: :toctree: generated/ roots_legendre -- Gauss-Legendre quadrature. roots_chebyt -- Gauss-Chebyshev (first kind) quadrature. roots_chebyu -- Gauss-Chebyshev (second kind) quadrature. roots_chebyc -- Gauss-Chebyshev (first kind) quadrature. roots_chebys -- Gauss-Chebyshev (second kind) quadrature. roots_jacobi -- Gauss-Jacobi quadrature. roots_laguerre -- Gauss-Laguerre quadrature. roots_genlaguerre -- Gauss-generalized Laguerre quadrature. roots_hermite -- Gauss-Hermite (physicst's) quadrature. roots_hermitenorm -- Gauss-Hermite (statistician's) quadrature. roots_gegenbauer -- Gauss-Gegenbauer quadrature. roots_sh_legendre -- Gauss-Legendre (shifted) quadrature. roots_sh_chebyt -- Gauss-Chebyshev (first kind, shifted) quadrature. roots_sh_chebyu -- Gauss-Chebyshev (second kind, shifted) quadrature. roots_sh_jacobi -- Gauss-Jacobi (shifted) quadrature. The functions below, in turn, return the polynomial coefficients in :class:`~.orthopoly1d` objects, which function similarly as :ref:`numpy.poly1d`. The :class:`~.orthopoly1d` class also has an attribute ``weights`` which returns the roots, weights, and total weights for the appropriate form of Gaussian quadrature. These are returned in an ``n x 3`` array with roots in the first column, weights in the second column, and total weights in the final column. Note that :class:`~.orthopoly1d` objects are converted to ``poly1d`` when doing arithmetic, and lose information of the original orthogonal polynomial. .. autosummary:: :toctree: generated/ legendre -- [+]Legendre polynomial. chebyt -- [+]Chebyshev polynomial of the first kind. chebyu -- [+]Chebyshev polynomial of the second kind. chebyc -- [+]Chebyshev polynomial of the first kind on :math:`[-2, 2]`. chebys -- [+]Chebyshev polynomial of the second kind on :math:`[-2, 2]`. jacobi -- [+]Jacobi polynomial. laguerre -- [+]Laguerre polynomial. genlaguerre -- [+]Generalized (associated) Laguerre polynomial. hermite -- [+]Physicist's Hermite polynomial. hermitenorm -- [+]Normalized (probabilist's) Hermite polynomial. gegenbauer -- [+]Gegenbauer (ultraspherical) polynomial. sh_legendre -- [+]Shifted Legendre polynomial. sh_chebyt -- [+]Shifted Chebyshev polynomial of the first kind. sh_chebyu -- [+]Shifted Chebyshev polynomial of the second kind. sh_jacobi -- [+]Shifted Jacobi polynomial. .. warning:: Computing values of high-order polynomials (around ``order > 20``) using polynomial coefficients is numerically unstable. To evaluate polynomial values, the ``eval_*`` functions should be used instead. Hypergeometric Functions ------------------------ .. autosummary:: :toctree: generated/ hyp2f1 -- Gauss hypergeometric function 2F1(a, b; c; z). hyp1f1 -- Confluent hypergeometric function 1F1(a, b; x) hyperu -- Confluent hypergeometric function U(a, b, x) of the second kind hyp0f1 -- Confluent hypergeometric limit function 0F1. hyp2f0 -- Hypergeometric function 2F0 in y and an error estimate hyp1f2 -- Hypergeometric function 1F2 and error estimate hyp3f0 -- Hypergeometric function 3F0 in y and an error estimate Parabolic Cylinder Functions ---------------------------- .. autosummary:: :toctree: generated/ pbdv -- Parabolic cylinder function D pbvv -- Parabolic cylinder function V pbwa -- Parabolic cylinder function W These are not universal functions: .. autosummary:: :toctree: generated/ pbdv_seq -- [+]Parabolic cylinder functions Dv(x) and derivatives. pbvv_seq -- [+]Parabolic cylinder functions Vv(x) and derivatives. pbdn_seq -- [+]Parabolic cylinder functions Dn(z) and derivatives. Mathieu and Related Functions ----------------------------- .. autosummary:: :toctree: generated/ mathieu_a -- Characteristic value of even Mathieu functions mathieu_b -- Characteristic value of odd Mathieu functions These are not universal functions: .. autosummary:: :toctree: generated/ mathieu_even_coef -- [+]Fourier coefficients for even Mathieu and modified Mathieu functions. mathieu_odd_coef -- [+]Fourier coefficients for even Mathieu and modified Mathieu functions. The following return both function and first derivative: .. autosummary:: :toctree: generated/ mathieu_cem -- Even Mathieu function and its derivative mathieu_sem -- Odd Mathieu function and its derivative mathieu_modcem1 -- Even modified Mathieu function of the first kind and its derivative mathieu_modcem2 -- Even modified Mathieu function of the second kind and its derivative mathieu_modsem1 -- Odd modified Mathieu function of the first kind and its derivative mathieu_modsem2 -- Odd modified Mathieu function of the second kind and its derivative Spheroidal Wave Functions ------------------------- .. autosummary:: :toctree: generated/ pro_ang1 -- Prolate spheroidal angular function of the first kind and its derivative pro_rad1 -- Prolate spheroidal radial function of the first kind and its derivative pro_rad2 -- Prolate spheroidal radial function of the secon kind and its derivative obl_ang1 -- Oblate spheroidal angular function of the first kind and its derivative obl_rad1 -- Oblate spheroidal radial function of the first kind and its derivative obl_rad2 -- Oblate spheroidal radial function of the second kind and its derivative. pro_cv -- Characteristic value of prolate spheroidal function obl_cv -- Characteristic value of oblate spheroidal function pro_cv_seq -- Characteristic values for prolate spheroidal wave functions. obl_cv_seq -- Characteristic values for oblate spheroidal wave functions. The following functions require pre-computed characteristic value: .. autosummary:: :toctree: generated/ pro_ang1_cv -- Prolate spheroidal angular function pro_ang1 for precomputed characteristic value pro_rad1_cv -- Prolate spheroidal radial function pro_rad1 for precomputed characteristic value pro_rad2_cv -- Prolate spheroidal radial function pro_rad2 for precomputed characteristic value obl_ang1_cv -- Oblate spheroidal angular function obl_ang1 for precomputed characteristic value obl_rad1_cv -- Oblate spheroidal radial function obl_rad1 for precomputed characteristic value obl_rad2_cv -- Oblate spheroidal radial function obl_rad2 for precomputed characteristic value Kelvin Functions ---------------- .. autosummary:: :toctree: generated/ kelvin -- Kelvin functions as complex numbers kelvin_zeros -- [+]Compute nt zeros of all Kelvin functions. ber -- Kelvin function ber. bei -- Kelvin function bei berp -- Derivative of the Kelvin function `ber` beip -- Derivative of the Kelvin function `bei` ker -- Kelvin function ker kei -- Kelvin function ker kerp -- Derivative of the Kelvin function ker keip -- Derivative of the Kelvin function kei These are not universal functions: .. autosummary:: :toctree: generated/ ber_zeros -- [+]Compute nt zeros of the Kelvin function ber(x). bei_zeros -- [+]Compute nt zeros of the Kelvin function bei(x). berp_zeros -- [+]Compute nt zeros of the Kelvin function ber'(x). beip_zeros -- [+]Compute nt zeros of the Kelvin function bei'(x). ker_zeros -- [+]Compute nt zeros of the Kelvin function ker(x). kei_zeros -- [+]Compute nt zeros of the Kelvin function kei(x). kerp_zeros -- [+]Compute nt zeros of the Kelvin function ker'(x). keip_zeros -- [+]Compute nt zeros of the Kelvin function kei'(x). Combinatorics ------------- .. autosummary:: :toctree: generated/ comb -- [+]The number of combinations of N things taken k at a time. perm -- [+]Permutations of N things taken k at a time, i.e., k-permutations of N. Lambert W and Related Functions ------------------------------- .. autosummary:: :toctree: generated/ lambertw -- Lambert W function. wrightomega -- Wright Omega function. Other Special Functions ----------------------- .. autosummary:: :toctree: generated/ agm -- Arithmetic, Geometric Mean. bernoulli -- Bernoulli numbers B0..Bn (inclusive). binom -- Binomial coefficient diric -- Periodic sinc function, also called the Dirichlet function. euler -- Euler numbers E0..En (inclusive). expn -- Exponential integral E_n exp1 -- Exponential integral E_1 of complex argument z expi -- Exponential integral Ei factorial -- The factorial of a number or array of numbers. factorial2 -- Double factorial. factorialk -- [+]Multifactorial of n of order k, n(!!...!). shichi -- Hyperbolic sine and cosine integrals. sici -- Sine and cosine integrals. spence -- Spence's function, also known as the dilogarithm. zeta -- Riemann zeta function. zetac -- Riemann zeta function minus 1. Convenience Functions --------------------- .. autosummary:: :toctree: generated/ cbrt -- Cube root of `x` exp10 -- 10**x exp2 -- 2**x radian -- Convert from degrees to radians cosdg -- Cosine of the angle `x` given in degrees. sindg -- Sine of angle given in degrees tandg -- Tangent of angle x given in degrees. cotdg -- Cotangent of the angle `x` given in degrees. log1p -- Calculates log(1+x) for use when `x` is near zero expm1 -- exp(x) - 1 for use when `x` is near zero. cosm1 -- cos(x) - 1 for use when `x` is near zero. round -- Round to nearest integer xlogy -- Compute ``x*log(y)`` so that the result is 0 if ``x = 0``. xlog1py -- Compute ``x*log1p(y)`` so that the result is 0 if ``x = 0``. logsumexp -- Compute the log of the sum of exponentials of input elements. exprel -- Relative error exponential, (exp(x)-1)/x, for use when `x` is near zero. sinc -- Return the sinc function. .. [+] in the description indicates a function which is not a universal .. function and does not follow broadcasting and automatic .. array-looping rules. """
"""Stuff to parse AIFF-C and AIFF files. Unless explicitly stated otherwise, the description below is true both for AIFF-C files and AIFF files. An AIFF-C file has the following structure. +-----------------+ | FORM | +-----------------+ | <size> | +----+------------+ | | AIFC | | +------------+ | | <chunks> | | | . | | | . | | | . | +----+------------+ An AIFF file has the string "AIFF" instead of "AIFC". A chunk consists of an identifier (4 bytes) followed by a size (4 bytes, big endian order), followed by the data. The size field does not include the size of the 8 byte header. The following chunk types are recognized. FVER <version number of AIFF-C defining document> (AIFF-C only). MARK <# of markers> (2 bytes) list of markers: <marker ID> (2 bytes, must be > 0) <position> (4 bytes) <marker name> ("pstring") COMM <# of channels> (2 bytes) <# of sound frames> (4 bytes) <size of the samples> (2 bytes) <sampling frequency> (10 bytes, IEEE 80-bit extended floating point) in AIFF-C files only: <compression type> (4 bytes) <human-readable version of compression type> ("pstring") SSND <offset> (4 bytes, not used by this program) <blocksize> (4 bytes, not used by this program) <sound data> A pstring consists of 1 byte length, a string of characters, and 0 or 1 byte pad to make the total length even. Usage. Reading AIFF files: f = aifc.open(file, 'r') where file is either the name of a file or an open file pointer. The open file pointer must have methods read(), seek(), and close(). In some types of audio files, if the setpos() method is not used, the seek() method is not necessary. This returns an instance of a class with the following public methods: getnchannels() -- returns number of audio channels (1 for mono, 2 for stereo) getsampwidth() -- returns sample width in bytes getframerate() -- returns sampling frequency getnframes() -- returns number of audio frames getcomptype() -- returns compression type ('NONE' for AIFF files) getcompname() -- returns human-readable version of compression type ('not compressed' for AIFF files) getparams() -- returns a namedtuple consisting of all of the above in the above order getmarkers() -- get the list of marks in the audio file or None if there are no marks getmark(id) -- get mark with the specified id (raises an error if the mark does not exist) readframes(n) -- returns at most n frames of audio rewind() -- rewind to the beginning of the audio stream setpos(pos) -- seek to the specified position tell() -- return the current position close() -- close the instance (make it unusable) The position returned by tell(), the position given to setpos() and the position of marks are all compatible and have nothing to do with the actual position in the file. The close() method is called automatically when the class instance is destroyed. Writing AIFF files: f = aifc.open(file, 'w') where file is either the name of a file or an open file pointer. The open file pointer must have methods write(), tell(), seek(), and close(). This returns an instance of a class with the following public methods: aiff() -- create an AIFF file (AIFF-C default) aifc() -- create an AIFF-C file setnchannels(n) -- set the number of channels setsampwidth(n) -- set the sample width setframerate(n) -- set the frame rate setnframes(n) -- set the number of frames setcomptype(type, name) -- set the compression type and the human-readable compression type setparams(tuple) -- set all parameters at once setmark(id, pos, name) -- add specified mark to the list of marks tell() -- return current position in output file (useful in combination with setmark()) writeframesraw(data) -- write audio frames without pathing up the file header writeframes(data) -- write audio frames and patch up the file header close() -- patch up the file header and close the output file You should set the parameters before the first writeframesraw or writeframes. The total number of frames does not need to be set, but when it is set to the correct value, the header does not have to be patched up. It is best to first set all parameters, perhaps possibly the compression type, and then write audio frames using writeframesraw. When all frames have been written, either call writeframes('') or close() to patch up the sizes in the header. Marks can be added anytime. If there are any marks, you must call close() after all frames have been written. The close() method is called automatically when the class instance is destroyed. When a file is opened with the extension '.aiff', an AIFF file is written, otherwise an AIFF-C file is written. This default can be changed by calling aiff() or aifc() before the first writeframes or writeframesraw. """
""" Objects for dealing with Chebyshev series. This module provides a number of objects (mostly functions) useful for dealing with Chebyshev series, including a `Chebyshev` class that encapsulates the usual arithmetic operations. (General information on how this module represents and works with such polynomials is in the docstring for its "parent" sub-package, `numpy.polynomial`). Constants --------- - `chebdomain` -- Chebyshev series default domain, [-1,1]. - `chebzero` -- (Coefficients of the) Chebyshev series that evaluates identically to 0. - `chebone` -- (Coefficients of the) Chebyshev series that evaluates identically to 1. - `chebx` -- (Coefficients of the) Chebyshev series for the identity map, ``f(x) = x``. Arithmetic ---------- - `chebadd` -- add two Chebyshev series. - `chebsub` -- subtract one Chebyshev series from another. - `chebmul` -- multiply two Chebyshev series. - `chebdiv` -- divide one Chebyshev series by another. - `chebpow` -- raise a Chebyshev series to an positive integer power - `chebval` -- evaluate a Chebyshev series at given points. - `chebval2d` -- evaluate a 2D Chebyshev series at given points. - `chebval3d` -- evaluate a 3D Chebyshev series at given points. - `chebgrid2d` -- evaluate a 2D Chebyshev series on a Cartesian product. - `chebgrid3d` -- evaluate a 3D Chebyshev series on a Cartesian product. Calculus -------- - `chebder` -- differentiate a Chebyshev series. - `chebint` -- integrate a Chebyshev series. Misc Functions -------------- - `chebfromroots` -- create a Chebyshev series with specified roots. - `chebroots` -- find the roots of a Chebyshev series. - `chebvander` -- Vandermonde-like matrix for Chebyshev polynomials. - `chebvander2d` -- Vandermonde-like matrix for 2D power series. - `chebvander3d` -- Vandermonde-like matrix for 3D power series. - `chebgauss` -- Gauss-Chebyshev quadrature, points and weights. - `chebweight` -- Chebyshev weight function. - `chebcompanion` -- symmetrized companion matrix in Chebyshev form. - `chebfit` -- least-squares fit returning a Chebyshev series. - `chebpts1` -- Chebyshev points of the first kind. - `chebpts2` -- Chebyshev points of the second kind. - `chebtrim` -- trim leading coefficients from a Chebyshev series. - `chebline` -- Chebyshev series representing given straight line. - `cheb2poly` -- convert a Chebyshev series to a polynomial. - `poly2cheb` -- convert a polynomial to a Chebyshev series. Classes ------- - `Chebyshev` -- A Chebyshev series class. See also -------- `numpy.polynomial` Notes ----- The implementations of multiplication, division, integration, and differentiation use the algebraic identities [1]_: .. math :: T_n(x) = \\frac{z^n + z^{-n}}{2} \\\\ z\\frac{dx}{dz} = \\frac{z - z^{-1}}{2}. where .. math :: x = \\frac{z + z^{-1}}{2}. These identities allow a Chebyshev series to be expressed as a finite, symmetric Laurent series. In this module, this sort of Laurent series is referred to as a "z-series." References ---------- .. [1] NAME et al., "Combinatorial Trigonometry with Chebyshev Polynomials," *Journal of Statistical Planning and Inference 14*, 2008 (preprint: http://www.math.hmc.edu/~benjamin/papers/CombTrig.pdf, pg. 4) """
""" ============================= Subclassing ndarray in python ============================= Credits ------- This page is based with thanks on the wiki page on subclassing by NAME - http://www.scipy.org/Subclasses. Introduction ------------ Subclassing ndarray is relatively simple, but it has some complications compared to other Python objects. On this page we explain the machinery that allows you to subclass ndarray, and the implications for implementing a subclass. ndarrays and object creation ============================ Subclassing ndarray is complicated by the fact that new instances of ndarray classes can come about in three different ways. These are: #. Explicit constructor call - as in ``MySubClass(params)``. This is the usual route to Python instance creation. #. View casting - casting an existing ndarray as a given subclass #. New from template - creating a new instance from a template instance. Examples include returning slices from a subclassed array, creating return types from ufuncs, and copying arrays. See :ref:`new-from-template` for more details The last two are characteristics of ndarrays - in order to support things like array slicing. The complications of subclassing ndarray are due to the mechanisms numpy has to support these latter two routes of instance creation. .. _view-casting: View casting ------------ *View casting* is the standard ndarray mechanism by which you take an ndarray of any subclass, and return a view of the array as another (specified) subclass: >>> import numpy as np >>> # create a completely useless ndarray subclass >>> class C(np.ndarray): pass >>> # create a standard ndarray >>> arr = np.zeros((3,)) >>> # take a view of it, as our useless subclass >>> c_arr = arr.view(C) >>> type(c_arr) <class 'C'> .. _new-from-template: Creating new from template -------------------------- New instances of an ndarray subclass can also come about by a very similar mechanism to :ref:`view-casting`, when numpy finds it needs to create a new instance from a template instance. The most obvious place this has to happen is when you are taking slices of subclassed arrays. For example: >>> v = c_arr[1:] >>> type(v) # the view is of type 'C' <class 'C'> >>> v is c_arr # but it's a new instance False The slice is a *view* onto the original ``c_arr`` data. So, when we take a view from the ndarray, we return a new ndarray, of the same class, that points to the data in the original. There are other points in the use of ndarrays where we need such views, such as copying arrays (``c_arr.copy()``), creating ufunc output arrays (see also :ref:`array-wrap`), and reducing methods (like ``c_arr.mean()``. Relationship of view casting and new-from-template -------------------------------------------------- These paths both use the same machinery. We make the distinction here, because they result in different input to your methods. Specifically, :ref:`view-casting` means you have created a new instance of your array type from any potential subclass of ndarray. :ref:`new-from-template` means you have created a new instance of your class from a pre-existing instance, allowing you - for example - to copy across attributes that are particular to your subclass. Implications for subclassing ---------------------------- If we subclass ndarray, we need to deal not only with explicit construction of our array type, but also :ref:`view-casting` or :ref:`new-from-template`. Numpy has the machinery to do this, and this machinery that makes subclassing slightly non-standard. There are two aspects to the machinery that ndarray uses to support views and new-from-template in subclasses. The first is the use of the ``ndarray.__new__`` method for the main work of object initialization, rather then the more usual ``__init__`` method. The second is the use of the ``__array_finalize__`` method to allow subclasses to clean up after the creation of views and new instances from templates. A brief Python primer on ``__new__`` and ``__init__`` ===================================================== ``__new__`` is a standard Python method, and, if present, is called before ``__init__`` when we create a class instance. See the `python __new__ documentation <http://docs.python.org/reference/datamodel.html#object.__new__>`_ for more detail. For example, consider the following Python code: .. testcode:: class C(object): def __new__(cls, *args): print 'Cls in __new__:', cls print 'Args in __new__:', args return object.__new__(cls, *args) def __init__(self, *args): print 'type(self) in __init__:', type(self) print 'Args in __init__:', args meaning that we get: >>> c = C('hello') Cls in __new__: <class 'C'> Args in __new__: ('hello',) type(self) in __init__: <class 'C'> Args in __init__: ('hello',) When we call ``C('hello')``, the ``__new__`` method gets its own class as first argument, and the passed argument, which is the string ``'hello'``. After python calls ``__new__``, it usually (see below) calls our ``__init__`` method, with the output of ``__new__`` as the first argument (now a class instance), and the passed arguments following. As you can see, the object can be initialized in the ``__new__`` method or the ``__init__`` method, or both, and in fact ndarray does not have an ``__init__`` method, because all the initialization is done in the ``__new__`` method. Why use ``__new__`` rather than just the usual ``__init__``? Because in some cases, as for ndarray, we want to be able to return an object of some other class. Consider the following: .. testcode:: class D(C): def __new__(cls, *args): print 'D cls is:', cls print 'D args in __new__:', args return C.__new__(C, *args) def __init__(self, *args): # we never get here print 'In D __init__' meaning that: >>> obj = D('hello') D cls is: <class 'D'> D args in __new__: ('hello',) Cls in __new__: <class 'C'> Args in __new__: ('hello',) >>> type(obj) <class 'C'> The definition of ``C`` is the same as before, but for ``D``, the ``__new__`` method returns an instance of class ``C`` rather than ``D``. Note that the ``__init__`` method of ``D`` does not get called. In general, when the ``__new__`` method returns an object of class other than the class in which it is defined, the ``__init__`` method of that class is not called. This is how subclasses of the ndarray class are able to return views that preserve the class type. When taking a view, the standard ndarray machinery creates the new ndarray object with something like:: obj = ndarray.__new__(subtype, shape, ... where ``subdtype`` is the subclass. Thus the returned view is of the same class as the subclass, rather than being of class ``ndarray``. That solves the problem of returning views of the same type, but now we have a new problem. The machinery of ndarray can set the class this way, in its standard methods for taking views, but the ndarray ``__new__`` method knows nothing of what we have done in our own ``__new__`` method in order to set attributes, and so on. (Aside - why not call ``obj = subdtype.__new__(...`` then? Because we may not have a ``__new__`` method with the same call signature). The role of ``__array_finalize__`` ================================== ``__array_finalize__`` is the mechanism that numpy provides to allow subclasses to handle the various ways that new instances get created. Remember that subclass instances can come about in these three ways: #. explicit constructor call (``obj = MySubClass(params)``). This will call the usual sequence of ``MySubClass.__new__`` then (if it exists) ``MySubClass.__init__``. #. :ref:`view-casting` #. :ref:`new-from-template` Our ``MySubClass.__new__`` method only gets called in the case of the explicit constructor call, so we can't rely on ``MySubClass.__new__`` or ``MySubClass.__init__`` to deal with the view casting and new-from-template. It turns out that ``MySubClass.__array_finalize__`` *does* get called for all three methods of object creation, so this is where our object creation housekeeping usually goes. * For the explicit constructor call, our subclass will need to create a new ndarray instance of its own class. In practice this means that we, the authors of the code, will need to make a call to ``ndarray.__new__(MySubClass,...)``, or do view casting of an existing array (see below) * For view casting and new-from-template, the equivalent of ``ndarray.__new__(MySubClass,...`` is called, at the C level. The arguments that ``__array_finalize__`` recieves differ for the three methods of instance creation above. The following code allows us to look at the call sequences and arguments: .. testcode:: import numpy as np class C(np.ndarray): def __new__(cls, *args, **kwargs): print 'In __new__ with class %s' % cls return np.ndarray.__new__(cls, *args, **kwargs) def __init__(self, *args, **kwargs): # in practice you probably will not need or want an __init__ # method for your subclass print 'In __init__ with class %s' % self.__class__ def __array_finalize__(self, obj): print 'In array_finalize:' print ' self type is %s' % type(self) print ' obj type is %s' % type(obj) Now: >>> # Explicit constructor >>> c = C((10,)) In __new__ with class <class 'C'> In array_finalize: self type is <class 'C'> obj type is <type 'NoneType'> In __init__ with class <class 'C'> >>> # View casting >>> a = np.arange(10) >>> cast_a = a.view(C) In array_finalize: self type is <class 'C'> obj type is <type 'numpy.ndarray'> >>> # Slicing (example of new-from-template) >>> cv = c[:1] In array_finalize: self type is <class 'C'> obj type is <class 'C'> The signature of ``__array_finalize__`` is:: def __array_finalize__(self, obj): ``ndarray.__new__`` passes ``__array_finalize__`` the new object, of our own class (``self``) as well as the object from which the view has been taken (``obj``). As you can see from the output above, the ``self`` is always a newly created instance of our subclass, and the type of ``obj`` differs for the three instance creation methods: * When called from the explicit constructor, ``obj`` is ``None`` * When called from view casting, ``obj`` can be an instance of any subclass of ndarray, including our own. * When called in new-from-template, ``obj`` is another instance of our own subclass, that we might use to update the new ``self`` instance. Because ``__array_finalize__`` is the only method that always sees new instances being created, it is the sensible place to fill in instance defaults for new object attributes, among other tasks. This may be clearer with an example. Simple example - adding an extra attribute to ndarray ----------------------------------------------------- .. testcode:: import numpy as np class InfoArray(np.ndarray): def __new__(subtype, shape, dtype=float, buffer=None, offset=0, strides=None, order=None, info=None): # Create the ndarray instance of our type, given the usual # ndarray input arguments. This will call the standard # ndarray constructor, but return an object of our type. # It also triggers a call to InfoArray.__array_finalize__ obj = np.ndarray.__new__(subtype, shape, dtype, buffer, offset, strides, order) # set the new 'info' attribute to the value passed obj.info = info # Finally, we must return the newly created object: return obj def __array_finalize__(self, obj): # ``self`` is a new object resulting from # ndarray.__new__(InfoArray, ...), therefore it only has # attributes that the ndarray.__new__ constructor gave it - # i.e. those of a standard ndarray. # # We could have got to the ndarray.__new__ call in 3 ways: # From an explicit constructor - e.g. InfoArray(): # obj is None # (we're in the middle of the InfoArray.__new__ # constructor, and self.info will be set when we return to # InfoArray.__new__) if obj is None: return # From view casting - e.g arr.view(InfoArray): # obj is arr # (type(obj) can be InfoArray) # From new-from-template - e.g infoarr[:3] # type(obj) is InfoArray # # Note that it is here, rather than in the __new__ method, # that we set the default value for 'info', because this # method sees all creation of default objects - with the # InfoArray.__new__ constructor, but also with # arr.view(InfoArray). self.info = getattr(obj, 'info', None) # We do not need to return anything Using the object looks like this: >>> obj = InfoArray(shape=(3,)) # explicit constructor >>> type(obj) <class 'InfoArray'> >>> obj.info is None True >>> obj = InfoArray(shape=(3,), info='information') >>> obj.info 'information' >>> v = obj[1:] # new-from-template - here - slicing >>> type(v) <class 'InfoArray'> >>> v.info 'information' >>> arr = np.arange(10) >>> cast_arr = arr.view(InfoArray) # view casting >>> type(cast_arr) <class 'InfoArray'> >>> cast_arr.info is None True This class isn't very useful, because it has the same constructor as the bare ndarray object, including passing in buffers and shapes and so on. We would probably prefer the constructor to be able to take an already formed ndarray from the usual numpy calls to ``np.array`` and return an object. Slightly more realistic example - attribute added to existing array ------------------------------------------------------------------- Here is a class that takes a standard ndarray that already exists, casts as our type, and adds an extra attribute. .. testcode:: import numpy as np class RealisticInfoArray(np.ndarray): def __new__(cls, input_array, info=None): # Input array is an already formed ndarray instance # We first cast to be our class type obj = np.asarray(input_array).view(cls) # add the new attribute to the created instance obj.info = info # Finally, we must return the newly created object: return obj def __array_finalize__(self, obj): # see InfoArray.__array_finalize__ for comments if obj is None: return self.info = getattr(obj, 'info', None) So: >>> arr = np.arange(5) >>> obj = RealisticInfoArray(arr, info='information') >>> type(obj) <class 'RealisticInfoArray'> >>> obj.info 'information' >>> v = obj[1:] >>> type(v) <class 'RealisticInfoArray'> >>> v.info 'information' .. _array-wrap: ``__array_wrap__`` for ufuncs ------------------------------------------------------- ``__array_wrap__`` gets called at the end of numpy ufuncs and other numpy functions, to allow a subclass to set the type of the return value and update attributes and metadata. Let's show how this works with an example. First we make the same subclass as above, but with a different name and some print statements: .. testcode:: import numpy as np class MySubClass(np.ndarray): def __new__(cls, input_array, info=None): obj = np.asarray(input_array).view(cls) obj.info = info return obj def __array_finalize__(self, obj): print 'In __array_finalize__:' print ' self is %s' % repr(self) print ' obj is %s' % repr(obj) if obj is None: return self.info = getattr(obj, 'info', None) def __array_wrap__(self, out_arr, context=None): print 'In __array_wrap__:' print ' self is %s' % repr(self) print ' arr is %s' % repr(out_arr) # then just call the parent return np.ndarray.__array_wrap__(self, out_arr, context) We run a ufunc on an instance of our new array: >>> obj = MySubClass(np.arange(5), info='spam') In __array_finalize__: self is MySubClass([0, 1, 2, 3, 4]) obj is array([0, 1, 2, 3, 4]) >>> arr2 = np.arange(5)+1 >>> ret = np.add(arr2, obj) In __array_wrap__: self is MySubClass([0, 1, 2, 3, 4]) arr is array([1, 3, 5, 7, 9]) In __array_finalize__: self is MySubClass([1, 3, 5, 7, 9]) obj is MySubClass([0, 1, 2, 3, 4]) >>> ret MySubClass([1, 3, 5, 7, 9]) >>> ret.info 'spam' Note that the ufunc (``np.add``) has called the ``__array_wrap__`` method of the input with the highest ``__array_priority__`` value, in this case ``MySubClass.__array_wrap__``, with arguments ``self`` as ``obj``, and ``out_arr`` as the (ndarray) result of the addition. In turn, the default ``__array_wrap__`` (``ndarray.__array_wrap__``) has cast the result to class ``MySubClass``, and called ``__array_finalize__`` - hence the copying of the ``info`` attribute. This has all happened at the C level. But, we could do anything we wanted: .. testcode:: class SillySubClass(np.ndarray): def __array_wrap__(self, arr, context=None): return 'I lost your data' >>> arr1 = np.arange(5) >>> obj = arr1.view(SillySubClass) >>> arr2 = np.arange(5) >>> ret = np.multiply(obj, arr2) >>> ret 'I lost your data' So, by defining a specific ``__array_wrap__`` method for our subclass, we can tweak the output from ufuncs. The ``__array_wrap__`` method requires ``self``, then an argument - which is the result of the ufunc - and an optional parameter *context*. This parameter is returned by some ufuncs as a 3-element tuple: (name of the ufunc, argument of the ufunc, domain of the ufunc). ``__array_wrap__`` should return an instance of its containing class. See the masked array subclass for an implementation. In addition to ``__array_wrap__``, which is called on the way out of the ufunc, there is also an ``__array_prepare__`` method which is called on the way into the ufunc, after the output arrays are created but before any computation has been performed. The default implementation does nothing but pass through the array. ``__array_prepare__`` should not attempt to access the array data or resize the array, it is intended for setting the output array type, updating attributes and metadata, and performing any checks based on the input that may be desired before computation begins. Like ``__array_wrap__``, ``__array_prepare__`` must return an ndarray or subclass thereof or raise an error. Extra gotchas - custom ``__del__`` methods and ndarray.base ----------------------------------------------------------- One of the problems that ndarray solves is keeping track of memory ownership of ndarrays and their views. Consider the case where we have created an ndarray, ``arr`` and have taken a slice with ``v = arr[1:]``. The two objects are looking at the same memory. Numpy keeps track of where the data came from for a particular array or view, with the ``base`` attribute: >>> # A normal ndarray, that owns its own data >>> arr = np.zeros((4,)) >>> # In this case, base is None >>> arr.base is None True >>> # We take a view >>> v1 = arr[1:] >>> # base now points to the array that it derived from >>> v1.base is arr True >>> # Take a view of a view >>> v2 = v1[1:] >>> # base points to the view it derived from >>> v2.base is v1 True In general, if the array owns its own memory, as for ``arr`` in this case, then ``arr.base`` will be None - there are some exceptions to this - see the numpy book for more details. The ``base`` attribute is useful in being able to tell whether we have a view or the original array. This in turn can be useful if we need to know whether or not to do some specific cleanup when the subclassed array is deleted. For example, we may only want to do the cleanup if the original array is deleted, but not the views. For an example of how this can work, have a look at the ``memmap`` class in ``numpy.core``. """
"""Drag-and-drop support for Tkinter. This is very preliminary. I currently only support dnd *within* one application, between different windows (or within the same window). I an trying to make this as generic as possible -- not dependent on the use of a particular widget or icon type, etc. I also hope that this will work with Pmw. To enable an object to be dragged, you must create an event binding for it that starts the drag-and-drop process. Typically, you should bind <ButtonPress> to a callback function that you write. The function should call Tkdnd.dnd_start(source, event), where 'source' is the object to be dragged, and 'event' is the event that invoked the call (the argument to your callback function). Even though this is a class instantiation, the returned instance should not be stored -- it will be kept alive automatically for the duration of the drag-and-drop. When a drag-and-drop is already in process for the Tk interpreter, the call is *ignored*; this normally averts starting multiple simultaneous dnd processes, e.g. because different button callbacks all dnd_start(). The object is *not* necessarily a widget -- it can be any application-specific object that is meaningful to potential drag-and-drop targets. Potential drag-and-drop targets are discovered as follows. Whenever the mouse moves, and at the start and end of a drag-and-drop move, the Tk widget directly under the mouse is inspected. This is the target widget (not to be confused with the target object, yet to be determined). If there is no target widget, there is no dnd target object. If there is a target widget, and it has an attribute dnd_accept, this should be a function (or any callable object). The function is called as dnd_accept(source, event), where 'source' is the object being dragged (the object passed to dnd_start() above), and 'event' is the most recent event object (generally a <Motion> event; it can also be <ButtonPress> or <ButtonRelease>). If the dnd_accept() function returns something other than None, this is the new dnd target object. If dnd_accept() returns None, or if the target widget has no dnd_accept attribute, the target widget's parent is considered as the target widget, and the search for a target object is repeated from there. If necessary, the search is repeated all the way up to the root widget. If none of the target widgets can produce a target object, there is no target object (the target object is None). The target object thus produced, if any, is called the new target object. It is compared with the old target object (or None, if there was no old target widget). There are several cases ('source' is the source object, and 'event' is the most recent event object): - Both the old and new target objects are None. Nothing happens. - The old and new target objects are the same object. Its method dnd_motion(source, event) is called. - The old target object was None, and the new target object is not None. The new target object's method dnd_enter(source, event) is called. - The new target object is None, and the old target object is not None. The old target object's method dnd_leave(source, event) is called. - The old and new target objects differ and neither is None. The old target object's method dnd_leave(source, event), and then the new target object's method dnd_enter(source, event) is called. Once this is done, the new target object replaces the old one, and the Tk mainloop proceeds. The return value of the methods mentioned above is ignored; if they raise an exception, the normal exception handling mechanisms take over. The drag-and-drop processes can end in two ways: a final target object is selected, or no final target object is selected. When a final target object is selected, it will always have been notified of the potential drop by a call to its dnd_enter() method, as described above, and possibly one or more calls to its dnd_motion() method; its dnd_leave() method has not been called since the last call to dnd_enter(). The target is notified of the drop by a call to its method dnd_commit(source, event). If no final target object is selected, and there was an old target object, its dnd_leave(source, event) method is called to complete the dnd sequence. Finally, the source object is notified that the drag-and-drop process is over, by a call to source.dnd_end(target, event), specifying either the selected target object, or None if no target object was selected. The source object can use this to implement the commit action; this is sometimes simpler than to do it in the target's dnd_commit(). The target's dnd_commit() method could then simply be aliased to dnd_leave(). At any time during a dnd sequence, the application can cancel the sequence by calling the cancel() method on the object returned by dnd_start(). This will call dnd_leave() if a target is currently active; it will never call dnd_commit(). """
#! /usr/bin/python # Multistage table builder # (c) NAME 2008 ############################################################################## # This script was submitted to the PCRE project by NAME as part of # the upgrading of Unicode property support. The new code speeds up property # matching many times. The script is for the use of PCRE maintainers, to # generate the pcre_ucd.c file that contains a digested form of the Unicode # data tables. # # The script has now been upgraded to Python 3 for PCRE2, and should be run in # the maint subdirectory, using the command # # [python3] ./MultiStage2.py >../src/pcre2_ucd.c # # It requires four Unicode data tables, DerivedGeneralCategory.txt, # GraphemeBreakProperty.txt, Scripts.txt, and CaseFolding.txt, to be in the # Unicode.tables subdirectory. The first of these is found in the "extracted" # subdirectory of the Unicode database (UCD) on the Unicode web site; the # second is in the "auxiliary" subdirectory; the other two are directly in the # UCD directory. # # Minor modifications made to this script: # Added #! line at start # Removed tabs # Made it work with Python 2.4 by rewriting two statements that needed 2.5 # Consequent code tidy # Adjusted data file names to take from the Unicode.tables directory # Adjusted global table names by prefixing _pcre_. # Commented out stuff relating to the casefolding table, which isn't used; # removed completely in 2012. # Corrected size calculation # Add #ifndef SUPPORT_UCP to use dummy tables when no UCP support is needed. # Update for PCRE2: name changes, and SUPPORT_UCP is abolished. # # Major modifications made to this script: # Added code to add a grapheme break property field to records. # # Added code to search for sets of more than two characters that must match # each other caselessly. A new table is output containing these sets, and # offsets into the table are added to the main output records. This new # code scans CaseFolding.txt instead of UnicodeData.txt. # # Update for Python3: # . Processed with 2to3, but that didn't fix everything # . Changed string.strip to str.strip # . Added encoding='utf-8' to the open() call # . Inserted 'int' before blocksize/ELEMS_PER_LINE because an int is # required and the result of the division is a float # # The main tables generated by this script are used by macros defined in # pcre2_internal.h. They look up Unicode character properties using short # sequences of code that contains no branches, which makes for greater speed. # # Conceptually, there is a table of records (of type ucd_record), containing a # script number, character type, grapheme break type, offset to caseless # matching set, and offset to the character's other case for every character. # However, a real table covering all Unicode characters would be far too big. # It can be efficiently compressed by observing that many characters have the # same record, and many blocks of characters (taking 128 characters in a block) # have the same set of records as other blocks. This leads to a 2-stage lookup # process. # # This script constructs four tables. The ucd_caseless_sets table contains # lists of characters that all match each other caselessly. Each list is # in order, and is terminated by NOTACHAR (0xffffffff), which is larger than # any valid character. The first list is empty; this is used for characters # that are not part of any list. # # The ucd_records table contains one instance of every unique record that is # required. The ucd_stage1 table is indexed by a character's block number, and # yields what is in effect a "virtual" block number. The ucd_stage2 table is a # table of "virtual" blocks; each block is indexed by the offset of a character # within its own block, and the result is the offset of the required record. # # Example: lowercase "a" (U+0061) is in block 0 # lookup 0 in stage1 table yields 0 # lookup 97 in the first table in stage2 yields 16 # record 17 is { 33, 5, 11, 0, -32 } # 33 = ucp_Latin => Latin script # 5 = ucp_Ll => Lower case letter # 11 = ucp_gbOther => Grapheme break property "Other" # 0 => not part of a caseless set # -32 => Other case is U+0041 # # Almost all lowercase latin characters resolve to the same record. One or two # are different because they are part of a multi-character caseless set (for # example, k, K and the Kelvin symbol are such a set). # # Example: hiragana letter A (U+3042) is in block 96 (0x60) # lookup 96 in stage1 table yields 88 # lookup 66 in the 88th table in stage2 yields 467 # record 470 is { 26, 7, 11, 0, 0 } # 26 = ucp_Hiragana => Hiragana script # 7 = ucp_Lo => Other letter # 11 = ucp_gbOther => Grapheme break property "Other" # 0 => not part of a caseless set # 0 => No other case # # In these examples, no other blocks resolve to the same "virtual" block, as it # happens, but plenty of other blocks do share "virtual" blocks. # # There is a fourth table, maintained by hand, which translates from the # individual character types such as ucp_Cc to the general types like ucp_C. # # NAME 03 July 2008 # # 01-March-2010: Updated list of scripts for Unicode 5.2.0 # 30-April-2011: Updated list of scripts for Unicode 6.0.0 # July-2012: Updated list of scripts for Unicode 6.1.0 # 20-August-2012: Added scan of GraphemeBreakProperty.txt and added a new # field in the record to hold the value. Luckily, the # structure had a hole in it, so the resulting table is # not much bigger than before. # 18-September-2012: Added code for multiple caseless sets. This uses the # final hole in the structure. # 30-September-2012: Added RegionalIndicator break property from Unicode 6.2.0 # 13-May-2014: Updated for PCRE2 # 03-June-2014: Updated for Python 3 # 20-June-2014: Updated for Unicode 7.0.0 # 12-August-2014: Updated to put Unicode version into the file ##############################################################################
"""Generic socket server classes. This module tries to capture the various aspects of defining a server: For socket-based servers: - address family: - AF_INET{,6}: IP (Internet Protocol) sockets (default) - AF_UNIX: Unix domain sockets - others, e.g. AF_DECNET are conceivable (see <socket.h> - socket type: - SOCK_STREAM (reliable stream, e.g. TCP) - SOCK_DGRAM (datagrams, e.g. UDP) For request-based servers (including socket-based): - client address verification before further looking at the request (This is actually a hook for any processing that needs to look at the request before anything else, e.g. logging) - how to handle multiple requests: - synchronous (one request is handled at a time) - forking (each request is handled by a new process) - threading (each request is handled by a new thread) The classes in this module favor the server type that is simplest to write: a synchronous TCP/IP server. This is bad class design, but save some typing. (There's also the issue that a deep class hierarchy slows down method lookups.) There are five classes in an inheritance diagram, four of which represent synchronous servers of four types: +------------+ | BaseServer | +------------+ | v +-----------+ +------------------+ | TCPServer |------->| UnixStreamServer | +-----------+ +------------------+ | v +-----------+ +--------------------+ | UDPServer |------->| UnixDatagramServer | +-----------+ +--------------------+ Note that UnixDatagramServer derives from UDPServer, not from UnixStreamServer -- the only difference between an IP and a Unix stream server is the address family, which is simply repeated in both unix server classes. Forking and threading versions of each type of server can be created using the ForkingMixIn and ThreadingMixIn mix-in classes. For instance, a threading UDP server class is created as follows: class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass The Mix-in class must come first, since it overrides a method defined in UDPServer! Setting the various member variables also changes the behavior of the underlying server mechanism. To implement a service, you must derive a class from BaseRequestHandler and redefine its handle() method. You can then run various versions of the service by combining one of the server classes with your request handler class. The request handler class must be different for datagram or stream services. This can be hidden by using the request handler subclasses StreamRequestHandler or DatagramRequestHandler. Of course, you still have to use your head! For instance, it makes no sense to use a forking server if the service contains state in memory that can be modified by requests (since the modifications in the child process would never reach the initial state kept in the parent process and passed to each child). In this case, you can use a threading server, but you will probably have to use locks to avoid two requests that come in nearly simultaneous to apply conflicting changes to the server state. On the other hand, if you are building e.g. an HTTP server, where all data is stored externally (e.g. in the file system), a synchronous class will essentially render the service "deaf" while one request is being handled -- which may be for a very long time if a client is slow to reqd all the data it has requested. Here a threading or forking server is appropriate. In some cases, it may be appropriate to process part of a request synchronously, but to finish processing in a forked child depending on the request data. This can be implemented by using a synchronous server and doing an explicit fork in the request handler class handle() method. Another approach to handling multiple simultaneous requests in an environment that supports neither threads nor fork (or where these are too expensive or inappropriate for the service) is to maintain an explicit table of partially finished requests and to use select() to decide which request to work on next (or whether to handle a new incoming request). This is particularly important for stream services where each client can potentially be connected for a long time (if threads or subprocesses cannot be used). Future work: - Standard classes for Sun RPC (which uses either UDP or TCP) - Standard mix-in classes to implement various authentication and encryption schemes - Standard framework for select-based multiplexing XXX Open problems: - What to do with out-of-band data? BaseServer: - split generic "request" functionality out into BaseServer class. Copyright (C) 2000 NAME <EMAIL> example: read entries from a SQL database (requires overriding get_request() to return a table entry from the database). entry is processed by a RequestHandlerClass. """
# -*- coding: utf-8 -*- ################################################################################ # header_operations expanded v.1.0.1 # ################################################################################ # TABLE OF CONTENTS ################################################################################ # # [ Z00 ] Introduction and Credits. # [ Z01 ] Operation Modifiers. # [ Z02 ] Flow Control. # [ Z03 ] Mathematical Operations. # [ Z04 ] Script/Trigger Parameters and Results. # [ Z05 ] Keyboard and Mouse Input. # [ Z06 ] World Map. # [ Z07 ] Game Settings. # [ Z08 ] Factions. # [ Z09 ] Parties and Party Templates. # [ Z10 ] Troops. # [ Z11 ] Quests. # [ Z12 ] Items. # [ Z13 ] Sounds and Music Tracks. # [ Z14 ] Positions. # [ Z15 ] Game Notes. # [ Z16 ] Tableaus and Heraldics. # [ Z17 ] String Operations. # [ Z18 ] Output And Messages. # [ Z19 ] Game Control: Screens, Menus, Dialogs and Encounters. # [ Z20 ] Scenes and Missions. # [ Z21 ] Scene Props and Prop Instances. # [ Z22 ] Agents and Teams. # [ Z23 ] Presentations. # [ Z24 ] Multiplayer And Networking. # [ Z25 ] Remaining Esoteric Stuff. # [ Z26 ] Hardcoded Compiler-Related Code. # ################################################################################ ################################################################################ # [ Z00 ] INTRODUCTION AND CREDITS ################################################################################ # Everyone who has ever tried to mod Mount&Blade games knows perfectly well, # that the documentation for it's Module System is severely lacking. Warband # Module System, while introducing many new and useful operations, did not # improve considerably in the way of documentation. What's worse, a number of # outright errors and inconsistencies appeared between what was documented in # the comments to the header_operations.py file (which was the root source of # all Warband scripting documentation, whether you like it or not), and what # was actually implemented in the game engine. # Sooner or later someone was bound to dedicate some time and effort to fix # this problem by properly documenting the file. It just so happened that I # was the first person crazy enough to accept the challenge. # I have tried to make this file a self-sufficient source of information on # every operation that the Warband scripting engine knows of. Naturally I # failed - there are still many operations for which there is simply not # enough information, or operations with effects that have not yet been # thoroughly tested and confirmed. But as far as I know, there is currently # no other reference more exhaustive than this. I tried to make the file # useful to both seasoned scripters and complete newbies, and to a certain # degree this file can even serve as a tutorial into Warband scripting - # though it still won't replace the wealth of tutorials produced by the # Warband modding community. # I really hope you will find it useful as well. # NAME AKA NAME Jan 18th, 2012. # And the credits. # First of all, I should credit Taleworlds for the creation of this game and # it's Module System. Without them, I wouldn't be able to work on this file # so even though I'm often sceptical about their programming style and quality # of their code, they still did a damn good job delivering this game to all # of us. # And then I should credit many members from the Warband modding community # who have shared their knowledge and helped me clear out many uncertainties # and inconsistencies. Special credits (in no particular order) go to # cmpxchg8b, Caba'drin, SonKidd, MadVader, dunde, Ikaguia, MadocComadrin, # Cjkjvfnby, shokkueibu. ################################################################################ # [ Z01 ] OPERATION MODIFIERS ################################################################################
""" [6/25/2014] Challenge #168 [Intermediate] Block Count, Length & Area https://www.reddit.com/r/dailyprogrammer/comments/291x9h/6252014_challenge_168_intermediate_block_count/ #Description: In construction there comes a need to compute the length and area of a jobsite. The areas and lengths computed are used by estimators to price out the cost to build that jobsite. If for example a jobsite was a building with a parking lot and had concrete walkways and some nice pavers and landscaping it would be good to know the areas of all these and some lengths (for concrete curbs, landscape headerboard, etc) So for today's challenge we are going to automate the tedious process of calculating the length and area of aerial plans or photos. #ASCII Photo: To keep this within our scope we have converted the plans into an ASCII picture. We have scaled the plans so 1 character is a square with dimensions of 10 ft x 10 ft. The photo is case sensitive. so a "O" and "o" are 2 different blocks of areas to compute. #Blocks Counts, Lengths and Areas: Some shorthand to follow: * SF = square feet * LF = linear feet If you have the following picture. #### OOOO #### mmmm * # has a block count of 2. we have 2 areas not joined made up of # * O and m have a block count of 1. they only have 1 areas each made up of their ASCII character. * O has 4 blocks. Each block is 100 SF and so you have 400 SF of O. * O has a circumference length of that 1 block count of 100 LF. * m also has 4 blocks so there is 400 SF of m and circumference length of 100 LF * # has 2 block counts each of 4. So # has a total area of 800 SF and a total circumference length of 200 LF. Pay close attention to how "#" was handled. It was seen as being 2 areas made up of # but the final length and area adds them together even thou they not together. It recognizes the two areas by having a block count of 2 (2 non-joined areas made up of "#" characters) while the others only have a block count of 1. #Input: Your input is a 2-D ASCII picture. The ASCII characters used are any non-whitespace characters. ##Example: #### @@oo o*@! **** #Output: You give a Length and Area report of all the blocks. ##Example: (using the example input) Block Count, Length & Area Report ================================= #: Total SF (400), Total Circumference LF (100) - Found 1 block @: Total SF (300), Total Circumference LF (100) - Found 2 blocks o: Total SF (300), Total Circumference LF (100) - Found 2 blocks *: Total SF (500), Total Circumference LF (120) - Found 1 block !: Total SF (100), Total Circumference LF (40) - Found 1 block #Easy Mode (optional): Remove the need to compute the block count. Just focus on area and circumference length. #Challenge Input: So we have a "B" building. It has a "D" driveway. "O" and "o" landscaping. "c" concrete walks. "p" pavers. "V" & "v" valley gutters. @ and T tree planting. Finally we have # as Asphalt Paving. ooooooooooooooooooooooDDDDDooooooooooooooooooooooooooooo ooooooooooooooooooooooDDDDDooooooooooooooooooooooooooooo ooo##################o#####o#########################ooo o@o##################o#####o#########################ooo ooo##################o#####o#########################oTo o@o##################################################ooo ooo##################################################oTo o@o############ccccccccccccccccccccccc###############ooo pppppppppppppppcOOOOOOOOOOOOOOOOOOOOOc###############oTo o@o############cOBBBBBBBBBBBBBBBBBBBOc###############ooo ooo####V#######cOBBBBBBBBBBBBBBBBBBBOc###############oTo o@o####V#######cOBBBBBBBBBBBBBBBBBBBOc###############ooo ooo####V#######cOBBBBBBBBBBBBBBBBBBBOcpppppppppppppppppp o@o####V#######cOBBBBBBBBBBBBBBBBBBBOc###############ooo ooo####V#######cOBBBBBBBBBBBBBBBBBBBOc######v########oTo o@o####V#######cOBBBBBBBBBBBBBBBBBBBOc######v########ooo ooo####V#######cOOOOOOOOOOOOOOOOOOOOOc######v########oTo o@o####V#######ccccccccccccccccccccccc######v########ooo ooo####V#######ppppppppppppppppppppppp######v########oTo o@o############ppppppppppppppppppppppp###############ooo oooooooooooooooooooooooooooooooooooooooooooooooooooooooo oooooooooooooooooooooooooooooooooooooooooooooooooooooooo #FAQ: Diagonals do not connect. The small example shows this. The @ areas are 2 blocks and not 1 because of the Diagonal. """
""" Priority Queue with Binary Heaps: --------------------------------- Introduction: ------------ A priority queue acts like a queue in that you can dequeue and item by removing it from the front. However, in a priority queue the logical order of items inside the queue is determined by their priority. The highest priority items are at the front of the queue and the lowest priority items are at the back. The classic way to implement a priority queue is using a data structure called a binary heap. A binary heap will allow us to both enqueue and dequeue items in O(logn). The binary heap is interesting to study because when we diagram the heap it looks a lot like a tree, but when we implement it we only use a single list as an internal representation. The binary heap has two common variations: the 'min heap,' in which the smallest key is always at the front, and the 'max heap,' in which the largest key is always at the front. In this section, we will implement the min heap. Basic Operations List: --------------------- BinaryHeap(): creates a new, empty binary heap. insert(k): adds a new item to the heap. findMin(): returns the item with the minimum key value, leaving item in the heap. delMin(): returns the item with the minimum key value, removing the item from the list. isEmpty(): returns true if the heap is empty, false otherwise. size(): returns the number of items in the heap. buildHeap(list): builds a new heap from a list of keys. The Structure Property: ----------------------- In order to make our heap work effeciently, we will take advantage of the logarithmic nature of the tree to represent our heap. In order to guarantee logarithmic performance, we must keep our tree balanced. A balanced binary tree has roughly the same number of nodes in the left and right subtrees of the root. In our heap implemention we keep the tree balanced by creating a 'complete binary tree.' A complete binary tree is a tree in which each level has all of its nodes. The exception to this is the bottom of the tree, which we fill in from left to right. Interesting Property: --------------------- Another interesting property of a complete tree is that we can represent it using a single list. We do not need to use nodes and references or even lists of lists. Because the tree is complete, the left child of the parent (at position p) is is the node that is found a position 2p in the list. Similarly, the right child of the parent is at position 2p+1 in the list. The Heap Order Property: ------------------------ The 'heap order property' is as follows: In a heap, for every node x with parent p, the key in p is smaller than or equal to the key in x. Heap Operations: ---------------- We will begin our implemention of a binary heap with the constructor. Since the entire binary heap can be represented by a single list, all the constructor will do is initialize the list and an attribute currentSize to keep track of the current size of the heap. You will notice that an empty binary heap has a single zero as the first element of heapList and that this zero is not used, but is there so that a simple integer can be used in later methods. Insert method: -------------- The next method we will implement in 'insert.' The easiest, and most efficient, way to add an item to a list is to simply append the item to the end of the list. The good news about appending is that it guarantees that we will maintain the complete tree property. The bad news is that we will very likely violate the heap structure property. However, it is possible to write a method that will allow us to regain the heap structure property by comparing the newly added items with its parent. If the newly added item is less than its parent, then we can swap the item with its parent. Notice that when we percolate an item up, we are restoring the heap property between the newly added item and the parent. We are also perserving the heap property for any siblings. delMin method notes: -------------------- Since the heap property requires that the root of the tree be the smallest item in the tree, finding the minimum item is easy. The hard part of delMin is restoring full compliance with the heap structure and heap order properties after the root has been removed. We can restore our heap in two steps. 1. We will restore the root item by taking the last item in the list and moving it to the root position. 2. We will restore the heap order property by pushng the new root node down the tree to its proper position. In order to maintain the heap order property, all we need to do is swap the root with its smallest child less than the root. After the initial swap, we may repeat the swapping process with a node and its children until the node is swapped into a position on the tree where it is already less than both children. The code for percolating a node down the tree is found in the 'percDown' and 'minChild' methods. buildHeap method: ----------------- To finish our discussion of binary heaps, we will look at a method to build an entire heap from a list of keys. If we start with an entire list then we can build the whole heap in O(n) operations. We will start from the middle of the list. Although we start out in the middle of the tree and work our way back towards the root, the percDown method enusres that the largest child is always down the tree. Beacuse it is a complete binary tree, any nodes past the halfway point will be leaves and therefore have no children. """
""" Project Euler Problem 8 ======================= Find the greatest product of thirteen consecutive digits in the 1000-digit number. 73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450 """
""" Objects for dealing with Chebyshev series. This module provides a number of objects (mostly functions) useful for dealing with Chebyshev series, including a `Chebyshev` class that encapsulates the usual arithmetic operations. (General information on how this module represents and works with such polynomials is in the docstring for its "parent" sub-package, `numpy.polynomial`). Constants --------- - `chebdomain` -- Chebyshev series default domain, [-1,1]. - `chebzero` -- (Coefficients of the) Chebyshev series that evaluates identically to 0. - `chebone` -- (Coefficients of the) Chebyshev series that evaluates identically to 1. - `chebx` -- (Coefficients of the) Chebyshev series for the identity map, ``f(x) = x``. Arithmetic ---------- - `chebadd` -- add two Chebyshev series. - `chebsub` -- subtract one Chebyshev series from another. - `chebmul` -- multiply two Chebyshev series. - `chebdiv` -- divide one Chebyshev series by another. - `chebpow` -- raise a Chebyshev series to an positive integer power - `chebval` -- evaluate a Chebyshev series at given points. - `chebval2d` -- evaluate a 2D Chebyshev series at given points. - `chebval3d` -- evaluate a 3D Chebyshev series at given points. - `chebgrid2d` -- evaluate a 2D Chebyshev series on a Cartesian product. - `chebgrid3d` -- evaluate a 3D Chebyshev series on a Cartesian product. Calculus -------- - `chebder` -- differentiate a Chebyshev series. - `chebint` -- integrate a Chebyshev series. Misc Functions -------------- - `chebfromroots` -- create a Chebyshev series with specified roots. - `chebroots` -- find the roots of a Chebyshev series. - `chebvander` -- Vandermonde-like matrix for Chebyshev polynomials. - `chebvander2d` -- Vandermonde-like matrix for 2D power series. - `chebvander3d` -- Vandermonde-like matrix for 3D power series. - `chebgauss` -- Gauss-Chebyshev quadrature, points and weights. - `chebweight` -- Chebyshev weight function. - `chebcompanion` -- symmetrized companion matrix in Chebyshev form. - `chebfit` -- least-squares fit returning a Chebyshev series. - `chebpts1` -- Chebyshev points of the first kind. - `chebpts2` -- Chebyshev points of the second kind. - `chebtrim` -- trim leading coefficients from a Chebyshev series. - `chebline` -- Chebyshev series representing given straight line. - `cheb2poly` -- convert a Chebyshev series to a polynomial. - `poly2cheb` -- convert a polynomial to a Chebyshev series. Classes ------- - `Chebyshev` -- A Chebyshev series class. See also -------- `numpy.polynomial` Notes ----- The implementations of multiplication, division, integration, and differentiation use the algebraic identities [1]_: .. math :: T_n(x) = \\frac{z^n + z^{-n}}{2} \\\\ z\\frac{dx}{dz} = \\frac{z - z^{-1}}{2}. where .. math :: x = \\frac{z + z^{-1}}{2}. These identities allow a Chebyshev series to be expressed as a finite, symmetric Laurent series. In this module, this sort of Laurent series is referred to as a "z-series." References ---------- .. [1] NAME et al., "Combinatorial Trigonometry with Chebyshev Polynomials," *Journal of Statistical Planning and Inference 14*, 2008 (preprint: http://www.math.hmc.edu/~benjamin/papers/CombTrig.pdf, pg. 4) """
""" Locally Optimal Block Preconditioned Conjugate Gradient Method (LOBPCG) LOBPCG is a preconditioned eigensolver for large symmetric positive definite (SPD) generalized eigenproblems. Call the function lobpcg - see help for lobpcg.lobpcg. See also lobpcg.as2d, which can be used in the preconditioner (example below) Acknowledgements ---------------- lobpcg.py code was written by NAME Many thanks belong to NAME the author of the algorithm, for lots of advice and support. Examples -------- >>> # Solve A x = lambda B x with constraints and preconditioning. >>> n = 100 >>> vals = [nm.arange( n, dtype = nm.float64 ) + 1] >>> # Matrix A. >>> operatorA = spdiags( vals, 0, n, n ) >>> # Matrix B >>> operatorB = nm.eye( n, n ) >>> # Constraints. >>> Y = nm.eye( n, 3 ) >>> # Initial guess for eigenvectors, should have linearly independent >>> # columns. Column dimension = number of requested eigenvalues. >>> X = sc.rand( n, 3 ) >>> # Preconditioner - inverse of A. >>> ivals = [1./vals[0]] >>> def precond( x ): invA = spdiags( ivals, 0, n, n ) y = invA * x if sp.issparse( y ): y = y.toarray() return as2d( y ) >>> >>> # Alternative way of providing the same preconditioner. >>> #precond = spdiags( ivals, 0, n, n ) >>> >>> tt = time.clock() >>> eigs, vecs = lobpcg( X, operatorA, operatorB, blockVectorY = Y, >>> operatorT = precond, >>> residualTolerance = 1e-4, maxIterations = 40, >>> largest = False, verbosityLevel = 1 ) >>> print 'solution time:', time.clock() - tt >>> print eigs Notes ----- In the following ``n`` denotes the matrix size and ``m`` the number of required eigenvalues (smallest or largest). The LOBPCG code internally solves eigenproblems of the size 3``m`` on every iteration by calling the "standard" dense eigensolver, so if ``m`` is not small enough compared to ``n``, it does not make sense to call the LOBPCG code, but rather one should use the "standard" eigensolver, e.g. scipy or symeig function in this case. If one calls the LOBPCG algorithm for 5``m``>``n``, it will most likely break internally, so the code tries to call the standard function instead. It is not that n should be large for the LOBPCG to work, but rather the ratio ``n``/``m`` should be large. It you call the LOBPCG code with ``m``=1 and ``n``=10, it should work, though ``n`` is small. The method is intended for extremely large ``n``/``m``, see e.g., reference [28] in http://arxiv.org/abs/0705.2626 The convergence speed depends basically on two factors: 1. How well relatively separated the seeking eigenvalues are from the rest of the eigenvalues. One can try to vary ``m`` to make this better. 2. How well conditioned the problem is. This can be changed by using proper preconditioning. For example, a rod vibration test problem (under tests directory) is ill-conditioned for large ``n``, so convergence will be slow, unless efficient preconditioning is used. For this specific problem, a good simple preconditioner function would be a linear solve for A, which is easy to code since A is tridiagonal. References ---------- A. NAME Toward the Optimal Preconditioned Eigensolver: Locally Optimal Block Preconditioned Conjugate Gradient Method. SIAM Journal on Scientific Computing 23 (2001), no. 2, pp. 517-541. http://dx.doi.org/10.1137/S1064827500366124 A. NAME NAME NAME and NAME Block Locally Optimal Preconditioned Eigenvalue Xolvers (BLOPEX) in hypre and PETSc (2007). http://arxiv.org/abs/0705.2626 A. NAME C and MATLAB implementations: http://www-math.cudenver.edu/~aknyazev/software/BLOPEX/ """
"""Stuff to parse AIFF-C and AIFF files. Unless explicitly stated otherwise, the description below is true both for AIFF-C files and AIFF files. An AIFF-C file has the following structure. +-----------------+ | FORM | +-----------------+ | <size> | +----+------------+ | | AIFC | | +------------+ | | <chunks> | | | . | | | . | | | . | +----+------------+ An AIFF file has the string "AIFF" instead of "AIFC". A chunk consists of an identifier (4 bytes) followed by a size (4 bytes, big endian order), followed by the data. The size field does not include the size of the 8 byte header. The following chunk types are recognized. FVER <version number of AIFF-C defining document> (AIFF-C only). MARK <# of markers> (2 bytes) list of markers: <marker ID> (2 bytes, must be > 0) <position> (4 bytes) <marker name> ("pstring") COMM <# of channels> (2 bytes) <# of sound frames> (4 bytes) <size of the samples> (2 bytes) <sampling frequency> (10 bytes, IEEE 80-bit extended floating point) in AIFF-C files only: <compression type> (4 bytes) <human-readable version of compression type> ("pstring") SSND <offset> (4 bytes, not used by this program) <blocksize> (4 bytes, not used by this program) <sound data> A pstring consists of 1 byte length, a string of characters, and 0 or 1 byte pad to make the total length even. Usage. Reading AIFF files: f = aifc.open(file, 'r') where file is either the name of a file or an open file pointer. The open file pointer must have methods read(), seek(), and close(). In some types of audio files, if the setpos() method is not used, the seek() method is not necessary. This returns an instance of a class with the following public methods: getnchannels() -- returns number of audio channels (1 for mono, 2 for stereo) getsampwidth() -- returns sample width in bytes getframerate() -- returns sampling frequency getnframes() -- returns number of audio frames getcomptype() -- returns compression type ('NONE' for AIFF files) getcompname() -- returns human-readable version of compression type ('not compressed' for AIFF files) getparams() -- returns a tuple consisting of all of the above in the above order getmarkers() -- get the list of marks in the audio file or None if there are no marks getmark(id) -- get mark with the specified id (raises an error if the mark does not exist) readframes(n) -- returns at most n frames of audio rewind() -- rewind to the beginning of the audio stream setpos(pos) -- seek to the specified position tell() -- return the current position close() -- close the instance (make it unusable) The position returned by tell(), the position given to setpos() and the position of marks are all compatible and have nothing to do with the actual position in the file. The close() method is called automatically when the class instance is destroyed. Writing AIFF files: f = aifc.open(file, 'w') where file is either the name of a file or an open file pointer. The open file pointer must have methods write(), tell(), seek(), and close(). This returns an instance of a class with the following public methods: aiff() -- create an AIFF file (AIFF-C default) aifc() -- create an AIFF-C file setnchannels(n) -- set the number of channels setsampwidth(n) -- set the sample width setframerate(n) -- set the frame rate setnframes(n) -- set the number of frames setcomptype(type, name) -- set the compression type and the human-readable compression type setparams(tuple) -- set all parameters at once setmark(id, pos, name) -- add specified mark to the list of marks tell() -- return current position in output file (useful in combination with setmark()) writeframesraw(data) -- write audio frames without pathing up the file header writeframes(data) -- write audio frames and patch up the file header close() -- patch up the file header and close the output file You should set the parameters before the first writeframesraw or writeframes. The total number of frames does not need to be set, but when it is set to the correct value, the header does not have to be patched up. It is best to first set all parameters, perhaps possibly the compression type, and then write audio frames using writeframesraw. When all frames have been written, either call writeframes('') or close() to patch up the sizes in the header. Marks can be added anytime. If there are any marks, ypu must call close() after all frames have been written. The close() method is called automatically when the class instance is destroyed. When a file is opened with the extension '.aiff', an AIFF file is written, otherwise an AIFF-C file is written. This default can be changed by calling aiff() or aifc() before the first writeframes or writeframesraw. """
"""Configuration file parser. A configuration file consists of sections, lead by a "[section]" header, and followed by "name: value" entries, with continuations and such in the style of RFC 822. Intrinsic defaults can be specified by passing them into the ConfigParser constructor as a dictionary. class: ConfigParser -- responsible for parsing a list of configuration files, and managing the parsed database. methods: __init__(defaults=None, dict_type=_default_dict, allow_no_value=False, delimiters=('=', ':'), comment_prefixes=('#', ';'), inline_comment_prefixes=None, strict=True, empty_lines_in_values=True): Create the parser. When `defaults' is given, it is initialized into the dictionary or intrinsic defaults. The keys must be strings, the values must be appropriate for %()s string interpolation. When `dict_type' is given, it will be used to create the dictionary objects for the list of sections, for the options within a section, and for the default values. When `delimiters' is given, it will be used as the set of substrings that divide keys from values. When `comment_prefixes' is given, it will be used as the set of substrings that prefix comments in empty lines. Comments can be indented. When `inline_comment_prefixes' is given, it will be used as the set of substrings that prefix comments in non-empty lines. When `strict` is True, the parser won't allow for any section or option duplicates while reading from a single source (file, string or dictionary). Default is True. When `empty_lines_in_values' is False (default: True), each empty line marks the end of an option. Otherwise, internal empty lines of a multiline option are kept as part of the value. When `allow_no_value' is True (default: False), options without values are accepted; the value presented for these is None. sections() Return all the configuration section names, sans DEFAULT. has_section(section) Return whether the given section exists. has_option(section, option) Return whether the given option exists in the given section. options(section) Return list of configuration options for the named section. read(filenames, encoding=None) Read and parse the list of named configuration files, given by name. A single filename is also allowed. Non-existing files are ignored. Return list of successfully read files. read_file(f, filename=None) Read and parse one configuration file, given as a file object. The filename defaults to f.name; it is only used in error messages (if f has no `name' attribute, the string `<???>' is used). read_string(string) Read configuration from a given string. read_dict(dictionary) Read configuration from a dictionary. Keys are section names, values are dictionaries with keys and values that should be present in the section. If the used dictionary type preserves order, sections and their keys will be added in order. Values are automatically converted to strings. get(section, option, raw=False, vars=None, fallback=_UNSET) Return a string value for the named option. All % interpolations are expanded in the return values, based on the defaults passed into the constructor and the DEFAULT section. Additional substitutions may be provided using the `vars' argument, which must be a dictionary whose contents override any pre-existing defaults. If `option' is a key in `vars', the value from `vars' is used. getint(section, options, raw=False, vars=None, fallback=_UNSET) Like get(), but convert value to an integer. getfloat(section, options, raw=False, vars=None, fallback=_UNSET) Like get(), but convert value to a float. getboolean(section, options, raw=False, vars=None, fallback=_UNSET) Like get(), but convert value to a boolean (currently case insensitively defined as 0, false, no, off for False, and 1, true, yes, on for True). Returns False or True. items(section=_UNSET, raw=False, vars=None) If section is given, return a list of tuples with (name, value) for each option in the section. Otherwise, return a list of tuples with (section_name, section_proxy) for each section, including DEFAULTSECT. remove_section(section) Remove the given file section and all its options. remove_option(section, option) Remove the given option from the given section. set(section, option, value) Set the given option. write(fp, space_around_delimiters=True) Write the configuration state in .ini format. If `space_around_delimiters' is True (the default), delimiters between keys and values are surrounded by spaces. """
#!/usr/bin/env python # (c) 2013, NAME <EMAIL> # # This file is part of Ansible. # # Ansible is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Ansible is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Ansible. If not, see <http://www.gnu.org/licenses/>. # # # Author: NAME <EMAIL> # # Description: # This module queries local or remote Docker daemons and generates # inventory information. # # This plugin does not support targeting of specific hosts using the --host # flag. Instead, it queries the Docker API for each container, running # or not, and returns this data all once. # # The plugin returns the following custom attributes on Docker containers: # docker_args # docker_config # docker_created # docker_driver # docker_exec_driver # docker_host_config # docker_hostname_path # docker_hosts_path # docker_id # docker_image # docker_name # docker_network_settings # docker_path # docker_resolv_conf_path # docker_state # docker_volumes # docker_volumes_rw # # Requirements: # The docker-py module: https://github.com/dotcloud/docker-py # # Notes: # A config file can be used to configure this inventory module, and there # are several environment variables that can be set to modify the behavior # of the plugin at runtime: # DOCKER_CONFIG_FILE # DOCKER_HOST # DOCKER_VERSION # DOCKER_TIMEOUT # DOCKER_PRIVATE_SSH_PORT # DOCKER_DEFAULT_IP # # Environment Variables: # environment variable: DOCKER_CONFIG_FILE # description: # - A path to a Docker inventory hosts/defaults file in YAML format # - A sample file has been provided, colocated with the inventory # file called 'docker.yml' # required: false # default: Uses docker.docker.Client constructor defaults # environment variable: DOCKER_HOST # description: # - The socket on which to connect to a Docker daemon API # required: false # default: Uses docker.docker.Client constructor defaults # environment variable: DOCKER_VERSION # description: # - Version of the Docker API to use # default: Uses docker.docker.Client constructor defaults # required: false # environment variable: DOCKER_TIMEOUT # description: # - Timeout in seconds for connections to Docker daemon API # default: Uses docker.docker.Client constructor defaults # required: false # environment variable: DOCKER_PRIVATE_SSH_PORT # description: # - The private port (container port) on which SSH is listening # for connections # default: 22 # required: false # environment variable: DOCKER_DEFAULT_IP # description: # - This environment variable overrides the container SSH connection # IP address (aka, 'ansible_ssh_host') # # This option allows one to override the ansible_ssh_host whenever # Docker has exercised its default behavior of binding private ports # to all interfaces of the Docker host. This behavior, when dealing # with remote Docker hosts, does not allow Ansible to determine # a proper host IP address on which to connect via SSH to containers. # By default, this inventory module assumes all IP_ADDRESS-exposed # ports to be bound to localhost:<port>. To override this # behavior, for example, to bind a container's SSH port to the public # interface of its host, one must manually set this IP. # # It is preferable to begin to launch Docker containers with # ports exposed on publicly accessible IP addresses, particularly # if the containers are to be targeted by Ansible for remote # configuration, not accessible via localhost SSH connections. # # Docker containers can be explicitly exposed on IP addresses by # a) starting the daemon with the --ip argument # b) running containers with the -P/--publish ip::containerPort # argument # default: IP_ADDRESS if port exposed on IP_ADDRESS by Docker # required: false # # Examples: # Use the config file: # DOCKER_CONFIG_FILE=./docker.yml docker.py --list # # Connect to docker instance on localhost port 4243 # DOCKER_HOST=tcp://localhost:4243 docker.py --list # # Any container's ssh port exposed on IP_ADDRESS will mapped to # another IP address (where Ansible will attempt to connect via SSH) # DOCKER_DEFAULT_IP=IP_ADDRESS docker.py --list
"""Drag-and-drop support for Tkinter. This is very preliminary. I currently only support dnd *within* one application, between different windows (or within the same window). I am trying to make this as generic as possible -- not dependent on the use of a particular widget or icon type, etc. I also hope that this will work with Pmw. To enable an object to be dragged, you must create an event binding for it that starts the drag-and-drop process. Typically, you should bind <ButtonPress> to a callback function that you write. The function should call Tkdnd.dnd_start(source, event), where 'source' is the object to be dragged, and 'event' is the event that invoked the call (the argument to your callback function). Even though this is a class instantiation, the returned instance should not be stored -- it will be kept alive automatically for the duration of the drag-and-drop. When a drag-and-drop is already in process for the Tk interpreter, the call is *ignored*; this normally averts starting multiple simultaneous dnd processes, e.g. because different button callbacks all dnd_start(). The object is *not* necessarily a widget -- it can be any application-specific object that is meaningful to potential drag-and-drop targets. Potential drag-and-drop targets are discovered as follows. Whenever the mouse moves, and at the start and end of a drag-and-drop move, the Tk widget directly under the mouse is inspected. This is the target widget (not to be confused with the target object, yet to be determined). If there is no target widget, there is no dnd target object. If there is a target widget, and it has an attribute dnd_accept, this should be a function (or any callable object). The function is called as dnd_accept(source, event), where 'source' is the object being dragged (the object passed to dnd_start() above), and 'event' is the most recent event object (generally a <Motion> event; it can also be <ButtonPress> or <ButtonRelease>). If the dnd_accept() function returns something other than None, this is the new dnd target object. If dnd_accept() returns None, or if the target widget has no dnd_accept attribute, the target widget's parent is considered as the target widget, and the search for a target object is repeated from there. If necessary, the search is repeated all the way up to the root widget. If none of the target widgets can produce a target object, there is no target object (the target object is None). The target object thus produced, if any, is called the new target object. It is compared with the old target object (or None, if there was no old target widget). There are several cases ('source' is the source object, and 'event' is the most recent event object): - Both the old and new target objects are None. Nothing happens. - The old and new target objects are the same object. Its method dnd_motion(source, event) is called. - The old target object was None, and the new target object is not None. The new target object's method dnd_enter(source, event) is called. - The new target object is None, and the old target object is not None. The old target object's method dnd_leave(source, event) is called. - The old and new target objects differ and neither is None. The old target object's method dnd_leave(source, event), and then the new target object's method dnd_enter(source, event) is called. Once this is done, the new target object replaces the old one, and the Tk mainloop proceeds. The return value of the methods mentioned above is ignored; if they raise an exception, the normal exception handling mechanisms take over. The drag-and-drop processes can end in two ways: a final target object is selected, or no final target object is selected. When a final target object is selected, it will always have been notified of the potential drop by a call to its dnd_enter() method, as described above, and possibly one or more calls to its dnd_motion() method; its dnd_leave() method has not been called since the last call to dnd_enter(). The target is notified of the drop by a call to its method dnd_commit(source, event). If no final target object is selected, and there was an old target object, its dnd_leave(source, event) method is called to complete the dnd sequence. Finally, the source object is notified that the drag-and-drop process is over, by a call to source.dnd_end(target, event), specifying either the selected target object, or None if no target object was selected. The source object can use this to implement the commit action; this is sometimes simpler than to do it in the target's dnd_commit(). The target's dnd_commit() method could then simply be aliased to dnd_leave(). At any time during a dnd sequence, the application can cancel the sequence by calling the cancel() method on the object returned by dnd_start(). This will call dnd_leave() if a target is currently active; it will never call dnd_commit(). """
#!/usr/bin/env python # Copyright (c) 2015 ARM Limited # All rights reserved # # The license below extends only to copyright in the software and shall # not be construed as granting a license to any other intellectual # property including but not limited to intellectual property relating # to a hardware implementation of the functionality of the software # licensed hereunder. You may use the software subject to the license # terms below provided that you ensure that this notice is replicated # unmodified and in its entirety in all distributions of the software, # modified or unmodified, in source code or in binary form. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer; # redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution; # neither the name of the copyright holders nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # # Copyright 2008 Google Inc. All rights reserved. # http://code.google.com/p/protobuf/ # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: # # * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above # copyright notice, this list of conditions and the following disclaimer # in the documentation and/or other materials provided with the # distribution. # * Neither the name of Google Inc. nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # # Authors: NAME This script is used to dump ASCII traces of the instruction dependency # graph to protobuf format. # # The ASCII trace format uses one line per instruction with the format # instruction sequence number, (optional) pc, (optional) weight, type, # (optional) flags, (optional) physical addr, (optional) size, comp delay, # (repeated) order dependencies comma-separated, and (repeated) register # dependencies comma-separated. # # examples: # seq_num,[pc],[weight,]type,[p_addr,size,flags,]comp_delay:[rob_dep]: # [reg_dep] # 1,35652,1,COMP,8500:: # 2,35656,1,COMP,0:,1: # 3,35660,1,LOAD,1748752,4,74,500:,2: # 4,35660,1,COMP,0:,3: # 5,35664,1,COMP,3000::,4 # 6,35666,1,STORE,1748752,4,74,1000:,3:,4,5 # 7,35666,1,COMP,3000::,4 # 8,35670,1,STORE,1748748,4,74,0:,6,3:,7 # 9,35670,1,COMP,500::,7
"""Generic socket server classes. This module tries to capture the various aspects of defining a server: For socket-based servers: - address family: - AF_INET{,6}: IP (Internet Protocol) sockets (default) - AF_UNIX: Unix domain sockets - others, e.g. AF_DECNET are conceivable (see <socket.h> - socket type: - SOCK_STREAM (reliable stream, e.g. TCP) - SOCK_DGRAM (datagrams, e.g. UDP) For request-based servers (including socket-based): - client address verification before further looking at the request (This is actually a hook for any processing that needs to look at the request before anything else, e.g. logging) - how to handle multiple requests: - synchronous (one request is handled at a time) - forking (each request is handled by a new process) - threading (each request is handled by a new thread) The classes in this module favor the server type that is simplest to write: a synchronous TCP/IP server. This is bad class design, but save some typing. (There's also the issue that a deep class hierarchy slows down method lookups.) There are five classes in an inheritance diagram, four of which represent synchronous servers of four types: +------------+ | BaseServer | +------------+ | v +-----------+ +------------------+ | TCPServer |------->| UnixStreamServer | +-----------+ +------------------+ | v +-----------+ +--------------------+ | UDPServer |------->| UnixDatagramServer | +-----------+ +--------------------+ Note that UnixDatagramServer derives from UDPServer, not from UnixStreamServer -- the only difference between an IP and a Unix stream server is the address family, which is simply repeated in both unix server classes. Forking and threading versions of each type of server can be created using the ForkingMixIn and ThreadingMixIn mix-in classes. For instance, a threading UDP server class is created as follows: class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass The Mix-in class must come first, since it overrides a method defined in UDPServer! Setting the various member variables also changes the behavior of the underlying server mechanism. To implement a service, you must derive a class from BaseRequestHandler and redefine its handle() method. You can then run various versions of the service by combining one of the server classes with your request handler class. The request handler class must be different for datagram or stream services. This can be hidden by using the request handler subclasses StreamRequestHandler or DatagramRequestHandler. Of course, you still have to use your head! For instance, it makes no sense to use a forking server if the service contains state in memory that can be modified by requests (since the modifications in the child process would never reach the initial state kept in the parent process and passed to each child). In this case, you can use a threading server, but you will probably have to use locks to avoid two requests that come in nearly simultaneous to apply conflicting changes to the server state. On the other hand, if you are building e.g. an HTTP server, where all data is stored externally (e.g. in the file system), a synchronous class will essentially render the service "deaf" while one request is being handled -- which may be for a very long time if a client is slow to reqd all the data it has requested. Here a threading or forking server is appropriate. In some cases, it may be appropriate to process part of a request synchronously, but to finish processing in a forked child depending on the request data. This can be implemented by using a synchronous server and doing an explicit fork in the request handler class handle() method. Another approach to handling multiple simultaneous requests in an environment that supports neither threads nor fork (or where these are too expensive or inappropriate for the service) is to maintain an explicit table of partially finished requests and to use select() to decide which request to work on next (or whether to handle a new incoming request). This is particularly important for stream services where each client can potentially be connected for a long time (if threads or subprocesses cannot be used). Future work: - Standard classes for Sun RPC (which uses either UDP or TCP) - Standard mix-in classes to implement various authentication and encryption schemes - Standard framework for select-based multiplexing XXX Open problems: - What to do with out-of-band data? BaseServer: - split generic "request" functionality out into BaseServer class. Copyright (C) 2000 NAME <EMAIL> example: read entries from a SQL database (requires overriding get_request() to return a table entry from the database). entry is processed by a RequestHandlerClass. """
#!/usr/bin/python #Algoriths Project Part 1a #Omar NAME 441 #Dr. NAME NOTES #Python's built in pow function uses Binary Exponentiation and reducing modulo n to compute modular #exponentiation. This is the same algorithm as MODULAR-EXPONENTIATION(a,b,n) as used in the text #For large number mutliplication Python uses Karatsuba's method as discusssed in class #Encrypted using modulus of 2048 bits #Message Encrypted with Private Key = #549335432742725778252187541104443188156944438806863457411666058499398272260706426139538267238120336092084632198514701950566203930065985324580534295693425367212921830205866755643739579288731322322946366466576799796974416100601383412159359169170613839877922173796152893918170136479717941167924064476336789776106984955596378941959676443995574307557232184168653454435294749983774161045180981596162964832360087083009219442813368249004389009182055455524458934480504555947413171214222377987666294266525295763559510397442092718659910879958017424466509571661222667744582625838716048450963735149873220637697801126262181088272 #n = 2372112898706524098783243835606671423055801883554227254030743710505202283932667011668956139382911768876035660572032080308562219037288900124052316286309512108625859836958747947762092799677854295671866288119481685786760570903533545560435541052326183788082279075073373227880942687435505490994525413101260845901748238215480998501123816262694263026377952163660645333809073068011604416987281948409408692393376191358516220341631487894075618891499412550098438456600441042870219500840853342452184082591601805986792948794525871595912715813197678328912976549353915846570322821639411967156886422360861220109970600152445030560129 #public key e = KEY883919633554596704012915783900570809149483856078010145425692545878452812725561415102822918517227924598205956910940350062144643427460974258169951841328548095289498955467345087157904399185646775059360160689508306113707875539862799501027047474838298216312008836598256088581250099042957573530717659415412893768343977899980494510094815770699761034869232518446869348437561961594909995056962983992121384916099020899755884457999313029602625570516932900789485878260172195900227111449085645227576679740196755445527867666825244974372425673866849078226602801561771006724501838806746943672716086807419555183315337s
""" ========================================== Statistical functions (:mod:`scipy.stats`) ========================================== .. module:: scipy.stats This module contains a large number of probability distributions as well as a growing library of statistical functions. Each univariate distribution is an instance of a subclass of `rv_continuous` (`rv_discrete` for discrete distributions): .. autosummary:: :toctree: generated/ rv_continuous rv_discrete Continuous distributions ======================== .. autosummary:: :toctree: generated/ alpha -- Alpha anglit -- Anglit arcsine -- Arcsine beta -- Beta betaprime -- Beta Prime bradford -- Bradford burr -- Burr cauchy -- Cauchy chi -- Chi chi2 -- Chi-squared cosine -- Cosine dgamma -- Double Gamma dweibull -- Double Weibull erlang -- Erlang expon -- Exponential exponnorm -- Exponentially Modified Normal exponweib -- Exponentiated Weibull exponpow -- Exponential Power f -- F (Snecdor F) fatiguelife -- Fatigue Life (Birnbaum-Saunders) fisk -- Fisk foldcauchy -- Folded Cauchy foldnorm -- Folded Normal frechet_r -- Frechet Right Sided, Extreme Value Type II (Extreme LB) or weibull_min frechet_l -- Frechet Left Sided, Weibull_max genlogistic -- Generalized Logistic gennorm -- Generalized normal genpareto -- Generalized Pareto genexpon -- Generalized Exponential genextreme -- Generalized Extreme Value gausshyper -- Gauss Hypergeometric gamma -- Gamma gengamma -- Generalized gamma genhalflogistic -- Generalized Half Logistic gilbrat -- Gilbrat gompertz -- Gompertz (Truncated Gumbel) gumbel_r -- Right Sided Gumbel, Log-Weibull, Fisher-Tippett, Extreme Value Type I gumbel_l -- Left Sided Gumbel, etc. halfcauchy -- Half Cauchy halflogistic -- Half Logistic halfnorm -- Half Normal halfgennorm -- Generalized Half Normal hypsecant -- Hyperbolic Secant invgamma -- Inverse Gamma invgauss -- Inverse Gaussian invweibull -- Inverse Weibull johnsonsb -- NAME johnsonsu -- NAME ksone -- Kolmogorov-Smirnov one-sided (no stats) kstwobign -- Kolmogorov-Smirnov two-sided test for Large N (no stats) laplace -- Laplace levy -- Levy levy_l levy_stable logistic -- Logistic loggamma -- Log-Gamma loglaplace -- Log-Laplace (Log Double Exponential) lognorm -- Log-Normal lomax -- Lomax (Pareto of the second kind) maxwell -- Maxwell mielke -- Mielke's Beta-Kappa nakagami -- Nakagami ncx2 -- Non-central chi-squared ncf -- Non-central F nct -- Non-central Student's T norm -- Normal (Gaussian) pareto -- Pareto pearson3 -- Pearson type III powerlaw -- Power-function powerlognorm -- Power log normal powernorm -- Power normal rdist -- R-distribution reciprocal -- Reciprocal rayleigh -- Rayleigh rice -- Rice recipinvgauss -- Reciprocal Inverse Gaussian semicircular -- Semicircular t -- Student's T triang -- Triangular truncexpon -- Truncated Exponential truncnorm -- Truncated Normal tukeylambda -- Tukey-Lambda uniform -- Uniform vonmises -- Von-Mises (Circular) vonmises_line -- Von-Mises (Line) wald -- Wald weibull_min -- Minimum Weibull (see Frechet) weibull_max -- Maximum Weibull (see Frechet) wrapcauchy -- Wrapped Cauchy Multivariate distributions ========================== .. autosummary:: :toctree: generated/ multivariate_normal -- Multivariate normal distribution dirichlet -- Dirichlet wishart -- Wishart invwishart -- Inverse Wishart Discrete distributions ====================== .. autosummary:: :toctree: generated/ bernoulli -- Bernoulli binom -- Binomial boltzmann -- Boltzmann (Truncated Discrete Exponential) dlaplace -- Discrete Laplacian geom -- Geometric hypergeom -- Hypergeometric logser -- Logarithmic (Log-Series, Series) nbinom -- Negative Binomial planck -- Planck (Discrete Exponential) poisson -- Poisson randint -- Discrete Uniform skellam -- Skellam zipf -- Zipf Statistical functions ===================== Several of these functions have a similar version in scipy.stats.mstats which work for masked arrays. .. autosummary:: :toctree: generated/ describe -- Descriptive statistics gmean -- Geometric mean hmean -- Harmonic mean kurtosis -- Fisher or Pearson kurtosis kurtosistest -- mode -- Modal value moment -- Central moment normaltest -- skew -- Skewness skewtest -- kstat -- kstatvar -- tmean -- Truncated arithmetic mean tvar -- Truncated variance tmin -- tmax -- tstd -- tsem -- nanmean -- Mean, ignoring NaN values nanstd -- Standard deviation, ignoring NaN values nanmedian -- Median, ignoring NaN values variation -- Coefficient of variation find_repeats trim_mean .. autosummary:: :toctree: generated/ cumfreq histogram2 histogram itemfreq percentileofscore scoreatpercentile relfreq .. autosummary:: :toctree: generated/ binned_statistic -- Compute a binned statistic for a set of data. binned_statistic_2d -- Compute a 2-D binned statistic for a set of data. binned_statistic_dd -- Compute a d-D binned statistic for a set of data. .. autosummary:: :toctree: generated/ obrientransform signaltonoise bayes_mvs mvsdist sem zmap zscore .. autosummary:: :toctree: generated/ sigmaclip threshold trimboth trim1 .. autosummary:: :toctree: generated/ f_oneway pearsonr spearmanr pointbiserialr kendalltau linregress theilslopes f_value .. autosummary:: :toctree: generated/ ttest_1samp ttest_ind ttest_ind_from_stats ttest_rel kstest chisquare power_divergence ks_2samp NAME tiecorrect rankdata ranksums wilcoxon kruskal friedmanchisquare combine_pvalues ss square_of_sums jarque_bera .. autosummary:: :toctree: generated/ ansari bartlett levene shapiro anderson anderson_ksamp binom_test fligner median_test mood .. autosummary:: :toctree: generated/ boxcox boxcox_normmax boxcox_llf entropy .. autosummary:: :toctree: generated/ chisqprob betai Circular statistical functions ============================== .. autosummary:: :toctree: generated/ circmean circvar circstd Contingency table functions =========================== .. autosummary:: :toctree: generated/ chi2_contingency contingency.expected_freq contingency.margins fisher_exact Plot-tests ========== .. autosummary:: :toctree: generated/ ppcc_max ppcc_plot probplot boxcox_normplot Masked statistics functions =========================== .. toctree:: stats.mstats Univariate and multivariate kernel density estimation (:mod:`scipy.stats.kde`) ============================================================================== .. autosummary:: :toctree: generated/ gaussian_kde For many more stat related functions install the software R and the interface package rpy. """
#!/usr/bin/env python # Try to determine how much RAM is currently being used per program. # Note per _program_, not per process. So for example this script # will report RAM used by all httpd process together. In detail it reports: # sum(private RAM for program processes) + sum(Shared RAM for program processes) # The shared RAM is problematic to calculate, and this script automatically # selects the most accurate method available for your kernel. # Licence: LGPLv2 # Author: EMAIL Source: http://www.pixelbeat.org/scripts/ps_mem.py # V1.0 06 Jul 2005 Initial release # V1.1 11 Aug 2006 root permission required for accuracy # V1.2 08 Nov 2006 Add total to output # Use KiB,MiB,... for units rather than K,M,... # V1.3 22 Nov 2006 Ignore shared col from /proc/$pid/statm for # 2.6 kernels up to and including 2.6.9. # There it represented the total file backed extent # V1.4 23 Nov 2006 Remove total from output as it's meaningless # (the shared values overlap with other programs). # Display the shared column. This extra info is # useful, especially as it overlaps between programs. # V1.5 26 Mar 2007 Remove redundant recursion from human() # V1.6 05 Jun 2007 Also report number of processes with a given name. # Patch from EMAIL V1.7 20 Sep 2007 Use PSS from /proc/$pid/smaps if available, which # fixes some over-estimation and allows totalling. # Enumerate the PIDs directly rather than using ps, # which fixes the possible race between reading # RSS with ps, and shared memory with this program. # Also we can show non truncated command names. # V1.8 28 Sep 2007 More accurate matching for stats in /proc/$pid/smaps # as otherwise could match libraries causing a crash. # Patch from EMAIL V1.9 20 Feb 2008 Fix invalid values reported when PSS is available. # Reported by NAME <EMAIL> # V3.3 24 Jun 2014 # http://github.com/pixelb/scripts/commits/master/scripts/ps_mem.py # Notes: # # All interpreted programs where the interpreter is started # by the shell or with env, will be merged to the interpreter # (as that's what's given to exec). For e.g. all python programs # starting with "#!/usr/bin/env python" will be grouped under python. # You can change this by using the full command line but that will # have the undesirable affect of splitting up programs started with # differing parameters (for e.g. mingetty tty[1-6]). # # For 2.6 kernels up to and including 2.6.13 and later 2.4 redhat kernels # (rmap vm without smaps) it can not be accurately determined how many pages # are shared between processes in general or within a program in our case: # http://lkml.org/lkml/2005/7/6/250 # A warning is printed if overestimation is possible. # In addition for 2.6 kernels up to 2.6.9 inclusive, the shared # value in /proc/$pid/statm is the total file-backed extent of a process. # We ignore that, introducing more overestimation, again printing a warning. # Since kernel 2.6.23-rc8-mm1 PSS is available in smaps, which allows # us to calculate a more accurate value for the total RAM used by programs. # # Programs that use CLONE_VM without CLONE_THREAD are discounted by assuming # they're the only programs that have the same /proc/$PID/smaps file for # each instance. This will fail if there are multiple real instances of a # program that then use CLONE_VM without CLONE_THREAD, or if a clone changes # its memory map while we're checksumming each /proc/$PID/smaps. # # I don't take account of memory allocated for a program # by other programs. For e.g. memory used in the X server for # a program could be determined, but is not. # # FreeBSD is supported if linprocfs is mounted at /compat/linux/proc/ # FreeBSD 8.0 supports up to a level of Linux 2.6.16
{'handleType': '0', 'rotate': (99.5, 137.5, 137.5, 137.5), 'baseSize_s': 0.1600000560283661, 'af2': 1.0, 'pruneRatio': 0.75, 'radiusTweak': (1.0, 1.0, 1.0, 1.0), 'pruneWidthPeak': 0.5, 'boneStep': (1, 1, 1, 1), 'nrings': 0, 'leafScale': 0.4000000059604645, 'makeMesh': False, 'baseSize': 0.30000001192092896, 'lengthV': (0.0, 0.10000000149011612, 0.0, 0.0), 'shapeS': '10', 'pruneBase': 0.11999999731779099, 'af3': 4.0, 'loopFrames': 0, 'horzLeaves': True, 'curveRes': (8, 5, 3, 1), 'minRadius': 0.001500000013038516, 'leafDist': '6', 'rotateV': (15.0, 0.0, 0.0, 0.0), 'bevel': True, 'curveBack': (0.0, 0.0, 0.0, 0.0), 'leafScaleV': 0.15000000596046448, 'prunePowerHigh': 0.5, 'rootFlare': 1.0, 'prune': False, 'branches': (0, 55, 10, 1), 'taperCrown': 0.5, 'useArm': False, 'splitBias': 0.5499999523162842, 'segSplits': (0.10000000149011612, 0.5, 0.20000000298023224, 0.0), 'resU': 4, 'useParentAngle': True, 'ratio': 0.014999999664723873, 'taper': (1.0, 1.0, 1.0, 1.0), 'length': (0.800000011920929, 0.6000000238418579, 0.5, 0.10000000149011612), 'scale0': 1.0, 'scaleV': 2.0, 'leafRotate': 137.5, 'shape': '7', 'scaleV0': 0.10000000149011612, 'leaves': 150, 'scale': 5.0, 'leafShape': 'hex', 'prunePowerLow': 0.0010000000474974513, 'splitAngle': (18.0, 18.0, 22.0, 0.0), 'seed': 0, 'showLeaves': True, 'downAngle': (0.0, 26.209999084472656, 52.55999755859375, 30.0), 'leafDownAngle': 30.0, 'autoTaper': True, 'rMode': 'rotate', 'leafScaleX': 0.20000000298023224, 'leafScaleT': 0.10000000149011612, 'gust': 1.0, 'armAnim': False, 'wind': 1.0, 'leafRotateV': 15.0, 'baseSplits': 3, 'attractOut': (0.0, 0.800000011920929, 0.0, 0.0), 'armLevels': 2, 'leafAnim': False, 'ratioPower': 1.2000000476837158, 'splitHeight': 0.20000000298023224, 'splitByLen': True, 'af1': 1.0, 'branchDist': 1.5, 'closeTip': False, 'previewArm': False, 'attractUp': (3.5, -1.899843692779541, 0.0, 0.0), 'bevelRes': 1, 'pruneWidth': 0.3400000035762787, 'gustF': 0.07500000298023224, 'leafangle': -12.0, 'curveV': (20.0, 50.0, 75.0, 0.0), 'useOldDownAngle': True, 'leafDownAngleV': -10.0, 'frameRate': 1.0, 'splitAngleV': (5.0, 5.0, 5.0, 0.0), 'levels': 2, 'downAngleV': (0.0, 10.0, 10.0, 10.0), 'customShape': (0.5, 1.0, 0.30000001192092896, 0.5), 'curve': (0.0, -15.0, 0.0, 0.0)}
# # XML-RPC CLIENT LIBRARY # $Id$ # # an XML-RPC client interface for Python. # # the marshalling and response parser code can also be used to # implement XML-RPC servers. # # Notes: # this version is designed to work with Python 2.1 or newer. # # History: # 1999-01-14 fl Created # 1999-01-15 fl Changed dateTime to use localtime # 1999-01-16 fl Added Binary/base64 element, default to RPC2 service # 1999-01-19 fl Fixed array data element (from Skip Montanaro) # 1999-01-21 fl Fixed dateTime constructor, etc. # 1999-02-02 fl Added fault handling, handle empty sequences, etc. # 1999-02-10 fl Fixed problem with empty responses (from Skip Montanaro) # 1999-06-20 fl Speed improvements, pluggable parsers/transports (0.9.8) # 2000-11-28 fl Changed boolean to check the truth value of its argument # 2001-02-24 fl Added encoding/Unicode/SafeTransport patches # 2001-02-26 fl Added compare support to wrappers (0.9.9/1.0b1) # 2001-03-28 fl Make sure response tuple is a singleton # 2001-03-29 fl Don't require empty params element (from NAME 2001-06-10 fl Folded in _xmlrpclib accelerator support (1.0b2) # 2001-08-20 fl Base xmlrpclib.Error on built-in Exception (from NAME 2001-09-03 fl Allow Transport subclass to override getparser # 2001-09-10 fl Lazy import of urllib, cgi, xmllib (20x import speedup) # 2001-10-01 fl Remove containers from memo cache when done with them # 2001-10-01 fl Use faster escape method (80% dumps speedup) # 2001-10-02 fl More dumps microtuning # 2001-10-04 fl Make sure import expat gets a parser (from NAME 2001-10-10 sm Allow long ints to be passed as ints if they don't overflow # 2001-10-17 sm Test for int and long overflow (allows use on 64-bit systems) # 2001-11-12 fl Use repr() to marshal doubles (from NAME 2002-03-17 fl Avoid buffered read when possible (from NAME 2002-04-07 fl Added pythondoc comments # 2002-04-16 fl Added __str__ methods to datetime/binary wrappers # 2002-05-15 fl Added error constants (from NAME 2002-06-27 fl Merged with Python CVS version # 2002-10-22 fl Added basic authentication (based on code from NAME 2003-01-22 sm Add support for the bool type # 2003-02-27 gvr Remove apply calls # 2003-04-24 sm Use cStringIO if available # 2003-04-25 ak Add support for nil # 2003-06-15 gn Add support for time.struct_time # 2003-07-12 gp Correct marshalling of Faults # 2003-10-31 mvl Add multicall support # 2004-08-20 mvl Bump minimum supported Python version to 2.1 # 2014-12-02 ch/doko Add workaround for gzip bomb vulnerability # # Copyright (c) 1999-2002 by Secret Labs AB. # Copyright (c) 1999-2002 by NAME Lundh. # # EMAIL http://www.pythonware.com # # -------------------------------------------------------------------- # The XML-RPC client interface is # # Copyright (c) 1999-2002 by Secret Labs AB # Copyright (c) 1999-2002 by NAME Lundh # # By obtaining, using, and/or copying this software and/or its # associated documentation, you agree that you have read, understood, # and will comply with the following terms and conditions: # # Permission to use, copy, modify, and distribute this software and # its associated documentation for any purpose and without fee is # hereby granted, provided that the above copyright notice appears in # all copies, and that both that copyright notice and this permission # notice appear in supporting documentation, and that the name of # Secret Labs AB or the author not be used in advertising or publicity # pertaining to distribution of the software without specific, written # prior permission. # # SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD # TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT- # ABILITY AND FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE # OF THIS SOFTWARE. # --------------------------------------------------------------------
""" =================== Universal Functions =================== Ufuncs are, generally speaking, mathematical functions or operations that are applied element-by-element to the contents of an array. That is, the result in each output array element only depends on the value in the corresponding input array (or arrays) and on no other array elements. NumPy comes with a large suite of ufuncs, and scipy extends that suite substantially. The simplest example is the addition operator: :: >>> np.array([0,2,3,4]) + np.array([1,1,-1,2]) array([1, 3, 2, 6]) The unfunc module lists all the available ufuncs in numpy. Documentation on the specific ufuncs may be found in those modules. This documentation is intended to address the more general aspects of unfuncs common to most of them. All of the ufuncs that make use of Python operators (e.g., +, -, etc.) have equivalent functions defined (e.g. add() for +) Type coercion ============= What happens when a binary operator (e.g., +,-,\\*,/, etc) deals with arrays of two different types? What is the type of the result? Typically, the result is the higher of the two types. For example: :: float32 + float64 -> float64 int8 + int32 -> int32 int16 + float32 -> float32 float32 + complex64 -> complex64 There are some less obvious cases generally involving mixes of types (e.g. uints, ints and floats) where equal bit sizes for each are not capable of saving all the information in a different type of equivalent bit size. Some examples are int32 vs float32 or uint32 vs int32. Generally, the result is the higher type of larger size than both (if available). So: :: int32 + float32 -> float64 uint32 + int32 -> int64 Finally, the type coercion behavior when expressions involve Python scalars is different than that seen for arrays. Since Python has a limited number of types, combining a Python int with a dtype=np.int8 array does not coerce to the higher type but instead, the type of the array prevails. So the rules for Python scalars combined with arrays is that the result will be that of the array equivalent the Python scalar if the Python scalar is of a higher 'kind' than the array (e.g., float vs. int), otherwise the resultant type will be that of the array. For example: :: Python int + int8 -> int8 Python float + int8 -> float64 ufunc methods ============= Binary ufuncs support 4 methods. **.reduce(arr)** applies the binary operator to elements of the array in sequence. For example: :: >>> np.add.reduce(np.arange(10)) # adds all elements of array 45 For multidimensional arrays, the first dimension is reduced by default: :: >>> np.add.reduce(np.arange(10).reshape(2,5)) array([ 5, 7, 9, 11, 13]) The axis keyword can be used to specify different axes to reduce: :: >>> np.add.reduce(np.arange(10).reshape(2,5),axis=1) array([10, 35]) **.accumulate(arr)** applies the binary operator and generates an an equivalently shaped array that includes the accumulated amount for each element of the array. A couple examples: :: >>> np.add.accumulate(np.arange(10)) array([ 0, 1, 3, 6, 10, 15, 21, 28, 36, 45]) >>> np.multiply.accumulate(np.arange(1,9)) array([ 1, 2, 6, 24, 120, 720, 5040, 40320]) The behavior for multidimensional arrays is the same as for .reduce(), as is the use of the axis keyword). **.reduceat(arr,indices)** allows one to apply reduce to selected parts of an array. It is a difficult method to understand. See the documentation at: **.outer(arr1,arr2)** generates an outer operation on the two arrays arr1 and arr2. It will work on multidimensional arrays (the shape of the result is the concatenation of the two input shapes.: :: >>> np.multiply.outer(np.arange(3),np.arange(4)) array([[0, 0, 0, 0], [0, 1, 2, 3], [0, 2, 4, 6]]) Output arguments ================ All ufuncs accept an optional output array. The array must be of the expected output shape. Beware that if the type of the output array is of a different (and lower) type than the output result, the results may be silently truncated or otherwise corrupted in the downcast to the lower type. This usage is useful when one wants to avoid creating large temporary arrays and instead allows one to reuse the same array memory repeatedly (at the expense of not being able to use more convenient operator notation in expressions). Note that when the output argument is used, the ufunc still returns a reference to the result. >>> x = np.arange(2) >>> np.add(np.arange(2),np.arange(2.),x) array([0, 2]) >>> x array([0, 2]) and & or as ufuncs ================== Invariably people try to use the python 'and' and 'or' as logical operators (and quite understandably). But these operators do not behave as normal operators since Python treats these quite differently. They cannot be overloaded with array equivalents. Thus using 'and' or 'or' with an array results in an error. There are two alternatives: 1) use the ufunc functions logical_and() and logical_or(). 2) use the bitwise operators & and \\|. The drawback of these is that if the arguments to these operators are not boolean arrays, the result is likely incorrect. On the other hand, most usages of logical_and and logical_or are with boolean arrays. As long as one is careful, this is a convenient way to apply these operators. """
# (c) 2013, NAME <EMAIL> red hat, inc # # This file is part of Ansible # # Ansible is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Ansible is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Ansible. If not, see <http://www.gnu.org/licenses/>. # take a list of files and (optionally) a list of paths # return the first existing file found in the paths # [file1, file2, file3], [path1, path2, path3] # search order is: # path1/file1 # path1/file2 # path1/file3 # path2/file1 # path2/file2 # path2/file3 # path3/file1 # path3/file2 # path3/file3 # first file found with os.path.exists() is returned # no file matches raises ansibleerror # EXAMPLES # - name: copy first existing file found to /some/file # action: copy src=$item dest=/some/file # with_first_found: # - files: foo ${inventory_hostname} bar # paths: /tmp/production /tmp/staging # that will look for files in this order: # /tmp/production/foo # ${inventory_hostname} # bar # /tmp/staging/foo # ${inventory_hostname} # bar # - name: copy first existing file found to /some/file # action: copy src=$item dest=/some/file # with_first_found: # - files: /some/place/foo ${inventory_hostname} /some/place/else # that will look for files in this order: # /some/place/foo # $relative_path/${inventory_hostname} # /some/place/else # example - including tasks: # tasks: # - include: $item # with_first_found: # - files: generic # paths: tasks/staging tasks/production # this will include the tasks in the file generic where it is found first (staging or production) # example simple file lists #tasks: #- name: first found file # action: copy src=$item dest=/etc/file.cfg # with_first_found: # - files: foo.${inventory_hostname} foo # example skipping if no matched files # First_found also offers the ability to control whether or not failing # to find a file returns an error or not # #- name: first found file - or skip # action: copy src=$item dest=/etc/file.cfg # with_first_found: # - files: foo.${inventory_hostname} # skip: true # example a role with default configuration and configuration per host # you can set multiple terms with their own files and paths to look through. # consider a role that sets some configuration per host falling back on a default config. # #- name: some configuration template # template: src={{ item }} dest=/etc/file.cfg mode=0444 owner=root group=root # with_first_found: # - files: # - ${inventory_hostname}/etc/file.cfg # paths: # - ../../../templates.overwrites # - ../../../templates # - files: # - etc/file.cfg # paths: # - templates # the above will return an empty list if the files cannot be found at all # if skip is unspecificed or if it is set to false then it will return a list # error which can be caught bye ignore_errors: true for that action. # finally - if you want you can use it, in place to replace first_available_file: # you simply cannot use the - files, path or skip options. simply replace # first_available_file with with_first_found and leave the file listing in place # # # - name: with_first_found like first_available_file # action: copy src=$item dest=/tmp/faftest # with_first_found: # - ../files/foo # - ../files/bar # - ../files/baz # ignore_errors: true
""" TestCmd.py: a testing framework for commands and scripts. The TestCmd module provides a framework for portable automated testing of executable commands and scripts (in any language, not just Python), especially commands and scripts that require file system interaction. In addition to running tests and evaluating conditions, the TestCmd module manages and cleans up one or more temporary workspace directories, and provides methods for creating files and directories in those workspace directories from in-line data, here-documents), allowing tests to be completely self-contained. A TestCmd environment object is created via the usual invocation: import TestCmd test = TestCmd.TestCmd() There are a bunch of keyword arguments available at instantiation: test = TestCmd.TestCmd(description = 'string', program = 'program_or_script_to_test', interpreter = 'script_interpreter', workdir = 'prefix', subdir = 'subdir', verbose = Boolean, match = default_match_function, diff = default_diff_function, combine = Boolean) There are a bunch of methods that let you do different things: test.verbose_set(1) test.description_set('string') test.program_set('program_or_script_to_test') test.interpreter_set('script_interpreter') test.interpreter_set(['script_interpreter', 'arg']) test.workdir_set('prefix') test.workdir_set('') test.workpath('file') test.workpath('subdir', 'file') test.subdir('subdir', ...) test.rmdir('subdir', ...) test.write('file', "contents\n") test.write(['subdir', 'file'], "contents\n") test.read('file') test.read(['subdir', 'file']) test.read('file', mode) test.read(['subdir', 'file'], mode) test.writable('dir', 1) test.writable('dir', None) test.preserve(condition, ...) test.cleanup(condition) test.command_args(program = 'program_or_script_to_run', interpreter = 'script_interpreter', arguments = 'arguments to pass to program') test.run(program = 'program_or_script_to_run', interpreter = 'script_interpreter', arguments = 'arguments to pass to program', chdir = 'directory_to_chdir_to', stdin = 'input to feed to the program\n') universal_newlines = True) p = test.start(program = 'program_or_script_to_run', interpreter = 'script_interpreter', arguments = 'arguments to pass to program', universal_newlines = None) test.finish(self, p) test.pass_test() test.pass_test(condition) test.pass_test(condition, function) test.fail_test() test.fail_test(condition) test.fail_test(condition, function) test.fail_test(condition, function, skip) test.no_result() test.no_result(condition) test.no_result(condition, function) test.no_result(condition, function, skip) test.stdout() test.stdout(run) test.stderr() test.stderr(run) test.symlink(target, link) test.banner(string) test.banner(string, width) test.diff(actual, expected) test.match(actual, expected) test.match_exact("actual 1\nactual 2\n", "expected 1\nexpected 2\n") test.match_exact(["actual 1\n", "actual 2\n"], ["expected 1\n", "expected 2\n"]) test.match_re("actual 1\nactual 2\n", regex_string) test.match_re(["actual 1\n", "actual 2\n"], list_of_regexes) test.match_re_dotall("actual 1\nactual 2\n", regex_string) test.match_re_dotall(["actual 1\n", "actual 2\n"], list_of_regexes) test.tempdir() test.tempdir('temporary-directory') test.sleep() test.sleep(seconds) test.where_is('foo') test.where_is('foo', 'PATH1:PATH2') test.where_is('foo', 'PATH1;PATH2', '.suffix3;.suffix4') test.unlink('file') test.unlink('subdir', 'file') The TestCmd module provides pass_test(), fail_test(), and no_result() unbound functions that report test results for use with the Aegis change management system. These methods terminate the test immediately, reporting PASSED, FAILED, or NO RESULT respectively, and exiting with status 0 (success), 1 or 2 respectively. This allows for a distinction between an actual failed test and a test that could not be properly evaluated because of an external condition (such as a full file system or incorrect permissions). import TestCmd TestCmd.pass_test() TestCmd.pass_test(condition) TestCmd.pass_test(condition, function) TestCmd.fail_test() TestCmd.fail_test(condition) TestCmd.fail_test(condition, function) TestCmd.fail_test(condition, function, skip) TestCmd.no_result() TestCmd.no_result(condition) TestCmd.no_result(condition, function) TestCmd.no_result(condition, function, skip) The TestCmd module also provides unbound functions that handle matching in the same way as the match_*() methods described above. import TestCmd test = TestCmd.TestCmd(match = TestCmd.match_exact) test = TestCmd.TestCmd(match = TestCmd.match_re) test = TestCmd.TestCmd(match = TestCmd.match_re_dotall) The TestCmd module provides unbound functions that can be used for the "diff" argument to TestCmd.TestCmd instantiation: import TestCmd test = TestCmd.TestCmd(match = TestCmd.match_re, diff = TestCmd.diff_re) test = TestCmd.TestCmd(diff = TestCmd.simple_diff) The "diff" argument can also be used with standard difflib functions: import difflib test = TestCmd.TestCmd(diff = difflib.context_diff) test = TestCmd.TestCmd(diff = difflib.unified_diff) Lastly, the where_is() method also exists in an unbound function version. import TestCmd TestCmd.where_is('foo') TestCmd.where_is('foo', 'PATH1:PATH2') TestCmd.where_is('foo', 'PATH1;PATH2', '.suffix3;.suffix4') """
""" TestCmd.py: a testing framework for commands and scripts. The TestCmd module provides a framework for portable automated testing of executable commands and scripts (in any language, not just Python), especially commands and scripts that require file system interaction. In addition to running tests and evaluating conditions, the TestCmd module manages and cleans up one or more temporary workspace directories, and provides methods for creating files and directories in those workspace directories from in-line data, here-documents), allowing tests to be completely self-contained. A TestCmd environment object is created via the usual invocation: import TestCmd test = TestCmd.TestCmd() There are a bunch of keyword arguments available at instantiation: test = TestCmd.TestCmd(description = 'string', program = 'program_or_script_to_test', interpreter = 'script_interpreter', workdir = 'prefix', subdir = 'subdir', verbose = Boolean, match = default_match_function, diff = default_diff_function, combine = Boolean) There are a bunch of methods that let you do different things: test.verbose_set(1) test.description_set('string') test.program_set('program_or_script_to_test') test.interpreter_set('script_interpreter') test.interpreter_set(['script_interpreter', 'arg']) test.workdir_set('prefix') test.workdir_set('') test.workpath('file') test.workpath('subdir', 'file') test.subdir('subdir', ...) test.rmdir('subdir', ...) test.write('file', "contents\n") test.write(['subdir', 'file'], "contents\n") test.read('file') test.read(['subdir', 'file']) test.read('file', mode) test.read(['subdir', 'file'], mode) test.writable('dir', 1) test.writable('dir', None) test.preserve(condition, ...) test.cleanup(condition) test.command_args(program = 'program_or_script_to_run', interpreter = 'script_interpreter', arguments = 'arguments to pass to program') test.run(program = 'program_or_script_to_run', interpreter = 'script_interpreter', arguments = 'arguments to pass to program', chdir = 'directory_to_chdir_to', stdin = 'input to feed to the program\n') universal_newlines = True) p = test.start(program = 'program_or_script_to_run', interpreter = 'script_interpreter', arguments = 'arguments to pass to program', universal_newlines = None) test.finish(self, p) test.pass_test() test.pass_test(condition) test.pass_test(condition, function) test.fail_test() test.fail_test(condition) test.fail_test(condition, function) test.fail_test(condition, function, skip) test.no_result() test.no_result(condition) test.no_result(condition, function) test.no_result(condition, function, skip) test.stdout() test.stdout(run) test.stderr() test.stderr(run) test.symlink(target, link) test.banner(string) test.banner(string, width) test.diff(actual, expected) test.match(actual, expected) test.match_exact("actual 1\nactual 2\n", "expected 1\nexpected 2\n") test.match_exact(["actual 1\n", "actual 2\n"], ["expected 1\n", "expected 2\n"]) test.match_re("actual 1\nactual 2\n", regex_string) test.match_re(["actual 1\n", "actual 2\n"], list_of_regexes) test.match_re_dotall("actual 1\nactual 2\n", regex_string) test.match_re_dotall(["actual 1\n", "actual 2\n"], list_of_regexes) test.tempdir() test.tempdir('temporary-directory') test.sleep() test.sleep(seconds) test.where_is('foo') test.where_is('foo', 'PATH1:PATH2') test.where_is('foo', 'PATH1;PATH2', '.suffix3;.suffix4') test.unlink('file') test.unlink('subdir', 'file') The TestCmd module provides pass_test(), fail_test(), and no_result() unbound functions that report test results for use with the Aegis change management system. These methods terminate the test immediately, reporting PASSED, FAILED, or NO RESULT respectively, and exiting with status 0 (success), 1 or 2 respectively. This allows for a distinction between an actual failed test and a test that could not be properly evaluated because of an external condition (such as a full file system or incorrect permissions). import TestCmd TestCmd.pass_test() TestCmd.pass_test(condition) TestCmd.pass_test(condition, function) TestCmd.fail_test() TestCmd.fail_test(condition) TestCmd.fail_test(condition, function) TestCmd.fail_test(condition, function, skip) TestCmd.no_result() TestCmd.no_result(condition) TestCmd.no_result(condition, function) TestCmd.no_result(condition, function, skip) The TestCmd module also provides unbound functions that handle matching in the same way as the match_*() methods described above. import TestCmd test = TestCmd.TestCmd(match = TestCmd.match_exact) test = TestCmd.TestCmd(match = TestCmd.match_re) test = TestCmd.TestCmd(match = TestCmd.match_re_dotall) The TestCmd module provides unbound functions that can be used for the "diff" argument to TestCmd.TestCmd instantiation: import TestCmd test = TestCmd.TestCmd(match = TestCmd.match_re, diff = TestCmd.diff_re) test = TestCmd.TestCmd(diff = TestCmd.simple_diff) The "diff" argument can also be used with standard difflib functions: import difflib test = TestCmd.TestCmd(diff = difflib.context_diff) test = TestCmd.TestCmd(diff = difflib.unified_diff) Lastly, the where_is() method also exists in an unbound function version. import TestCmd TestCmd.where_is('foo') TestCmd.where_is('foo', 'PATH1:PATH2') TestCmd.where_is('foo', 'PATH1;PATH2', '.suffix3;.suffix4') """
""" Basic functions used by several sub-packages and useful to have in the main name-space. Type Handling ------------- ================ =================== iscomplexobj Test for complex object, scalar result isrealobj Test for real object, scalar result iscomplex Test for complex elements, array result isreal Test for real elements, array result imag Imaginary part real Real part real_if_close Turns complex number with tiny imaginary part to real isneginf Tests for negative infinity, array result isposinf Tests for positive infinity, array result isnan Tests for nans, array result isinf Tests for infinity, array result isfinite Tests for finite numbers, array result isscalar True if argument is a scalar nan_to_num Replaces NaN's with 0 and infinities with large numbers cast Dictionary of functions to force cast to each type common_type Determine the minimum common type code for a group of arrays mintypecode Return minimal allowed common typecode. ================ =================== Index Tricks ------------ ================ =================== mgrid Method which allows easy construction of N-d 'mesh-grids' ``r_`` Append and construct arrays: turns slice objects into ranges and concatenates them, for 2d arrays appends rows. index_exp Konrad Hinsen's index_expression class instance which can be useful for building complicated slicing syntax. ================ =================== Useful Functions ---------------- ================ =================== select Extension of where to multiple conditions and choices extract Extract 1d array from flattened array according to mask insert Insert 1d array of values into Nd array according to mask linspace Evenly spaced samples in linear space logspace Evenly spaced samples in logarithmic space fix Round x to nearest integer towards zero mod Modulo mod(x,y) = x % y except keeps sign of y amax Array maximum along axis amin Array minimum along axis ptp Array max-min along axis cumsum Cumulative sum along axis prod Product of elements along axis cumprod Cumluative product along axis diff Discrete differences along axis angle Returns angle of complex argument unwrap Unwrap phase along given axis (1-d algorithm) sort_complex Sort a complex-array (based on real, then imaginary) trim_zeros Trim the leading and trailing zeros from 1D array. vectorize A class that wraps a Python function taking scalar arguments into a generalized function which can handle arrays of arguments using the broadcast rules of numerix Python. ================ =================== Shape Manipulation ------------------ ================ =================== squeeze Return a with length-one dimensions removed. atleast_1d Force arrays to be > 1D atleast_2d Force arrays to be > 2D atleast_3d Force arrays to be > 3D vstack Stack arrays vertically (row on row) hstack Stack arrays horizontally (column on column) column_stack Stack 1D arrays as columns into 2D array dstack Stack arrays depthwise (along third dimension) stack Stack arrays along a new axis split Divide array into a list of sub-arrays hsplit Split into columns vsplit Split into rows dsplit Split along third dimension ================ =================== Matrix (2D Array) Manipulations ------------------------------- ================ =================== fliplr 2D array with columns flipped flipud 2D array with rows flipped rot90 Rotate a 2D array a multiple of 90 degrees eye Return a 2D array with ones down a given diagonal diag Construct a 2D array from a vector, or return a given diagonal from a 2D array. mat Construct a Matrix bmat Build a Matrix from blocks ================ =================== Polynomials ----------- ================ =================== poly1d A one-dimensional polynomial class poly Return polynomial coefficients from roots roots Find roots of polynomial given coefficients polyint Integrate polynomial polyder Differentiate polynomial polyadd Add polynomials polysub Substract polynomials polymul Multiply polynomials polydiv Divide polynomials polyval Evaluate polynomial at given argument ================ =================== Import Tricks ------------- ================ =================== ppimport Postpone module import until trying to use it ppimport_attr Postpone module import until trying to use its attribute ppresolve Import postponed module and return it. ================ =================== Machine Arithmetics ------------------- ================ =================== machar_single Single precision floating point arithmetic parameters machar_double Double precision floating point arithmetic parameters ================ =================== Threading Tricks ---------------- ================ =================== ParallelExec Execute commands in parallel thread. ================ =================== 1D Array Set Operations ----------------------- Set operations for 1D numeric arrays based on sort() function. ================ =================== ediff1d Array difference (auxiliary function). unique Unique elements of an array. intersect1d Intersection of 1D arrays with unique elements. setxor1d Set exclusive-or of 1D arrays with unique elements. in1d Test whether elements in a 1D array are also present in another array. union1d Union of 1D arrays with unique elements. setdiff1d Set difference of 1D arrays with unique elements. ================ =================== """
""" Python Builtins --------------- Most buildin functions (that make sense in JS) are automatically translated to JavaScript: isinstance, issubclass, callable, hasattr, getattr, setattr, delattr, print, len, max, min, chr, ord, dict, list, tuple, range, pow, sum, round, int, float, str, bool, abs, divmod, all, any, enumerate, zip, reversed, sorted, filter, map. .. pyscript_example:: # "self" is replaced with "this" self.foo # Printing just works print('some test') print(a, b, c, sep='-') # Getting the length of a string or array len(foo) # Rounding and abs round(foo) # round to nearest integer int(foo) # round towards 0 as in Python abs(foo) # min and max min(foo) min(a, b, c) max(foo) max(a, b, c) # divmod a, b = divmod(100, 7) # -> 14, 2 # Aggregation sum(foo) all(foo) any(foo) # Turning things into numbers, bools and strings str(s) float(x) bool(y) int(z) # this rounds towards zero like in Python chr(65) # -> 'A' ord('A') # -> 65 # Turning things into lists and dicts dict([['foo', 1], ['bar', 2]]) # -> {'foo': 1, 'bar': 2} list('abc') # -> ['a', 'b', 'c'] dict(other_dict) # make a copy list(other_list) # make copy The isinstance function (and friends) ------------------------------------- The ``isinstance()`` function works for all JS primitive types, but also for user-defined classes. .. pyscript_example:: # Basic types isinstance(3, float) # in JS there are no ints isinstance('', str) isinstance([], list) isinstance({}, dict) isinstance(foo, types.FunctionType) # Can also use JS strings isinstance(3, 'number') isinstance('', 'string') isinstance([], 'array') isinstance({}, 'object') isinstance(foo, 'function') # You can use it on your own types too ... isinstance(x, MyClass) isinstance(x, 'MyClass') # equivalent isinstance(x, 'Object') # also yields true (subclass of Object) # issubclass works too issubclass(Foo, Bar) # As well as callable callable(foo) hasattr, getattr, setattr and delattr ------------------------------------- .. pyscript_example:: a = {'foo': 1, 'bar': 2} hasattr(a, 'foo') # -> True hasattr(a, 'fooo') # -> False hasattr(null, 'foo') # -> False getattr(a, 'foo') # -> 1 getattr(a, 'fooo') # -> raise AttributeError getattr(a, 'fooo', 3) # -> 3 getattr(null, 'foo', 3) # -> 3 setattr(a, 'foo', 2) delattr(a, 'foo') Creating sequences ------------------ .. pyscript_example:: range(10) range(2, 10, 2) range(100, 0, -1) reversed(foo) sorted(foo) enumerate(foo) zip(foo, bar) filter(func, foo) map(func, foo) List methods ------------ .. pyscript_example:: # Call a.append() if it exists, otherwise a.push() a.append(x) # Similar for remove() a.remove(x) Dict methods ------------ .. pyscript_example:: a = {'foo': 3} a['foo'] a.get('foo', 0) a.get('foo') a.keys() Str methods ----------- .. pyscript_example:: "foobar".startswith('foo') Additional sugar ---------------- .. pyscript_example:: # Get time (number of seconds since epoch) print(time.time()) # High resolution timer (as in time.perf_counter on Python 3) t0 = time.perf_counter() do_something() t1 = time.perf_counter() print('this took me', t1-t0, 'seconds') """
""" FASTA/QUAL format (:mod:`skbio.io.format.fasta`) ================================================ .. currentmodule:: skbio.io.format.fasta The FASTA file format (``fasta``) stores biological (i.e., nucleotide or protein) sequences in a simple plain text format that is both human-readable and easy to parse. The file format was first introduced and used in the FASTA software package [1]_. Additional descriptions of the file format can be found in [2]_ and [3]_. An example of a FASTA-formatted file containing two DNA sequences:: >seq1 db-accession-149855 CGATGTCGATCGATCGATCGATCAG >seq2 db-accession-34989 CATCGATCGATCGATGCATGCATGCATG The QUAL file format is an additional format related to FASTA. A FASTA file is sometimes accompanied by a QUAL file, particuarly when the FASTA file contains sequences generated on a high-throughput sequencing instrument. QUAL files store a Phred quality score (nonnegative integer) for each base in a sequence stored in FASTA format (see [4]_ for more details). scikit-bio supports reading and writing FASTA (and optionally QUAL) file formats. Format Support -------------- **Has Sniffer: Yes** +------+------+---------------------------------------------------------------+ |Reader|Writer| Object Class | +======+======+===============================================================+ |Yes |Yes |generator of :mod:`skbio.sequence.Sequence` objects | +------+------+---------------------------------------------------------------+ |Yes |Yes |:mod:`skbio.alignment.SequenceCollection` | +------+------+---------------------------------------------------------------+ |Yes |Yes |:mod:`skbio.alignment.Alignment` | +------+------+---------------------------------------------------------------+ |Yes |Yes |:mod:`skbio.sequence.Sequence` | +------+------+---------------------------------------------------------------+ |Yes |Yes |:mod:`skbio.sequence.DNA` | +------+------+---------------------------------------------------------------+ |Yes |Yes |:mod:`skbio.sequence.RNA` | +------+------+---------------------------------------------------------------+ |Yes |Yes |:mod:`skbio.sequence.Protein` | +------+------+---------------------------------------------------------------+ .. note:: All readers and writers support an optional QUAL file via the ``qual`` parameter. If one is provided, quality scores will be read/written in addition to FASTA sequence data. Format Specification -------------------- The following sections define the FASTA and QUAL file formats in detail. FASTA Format ^^^^^^^^^^^^ A FASTA file contains one or more biological sequences. The sequences are stored sequentially, with a *record* for each sequence (also referred to as a *FASTA record*). Each *record* consists of a single-line *header* (sometimes referred to as a *defline*, *label*, *description*, or *comment*) followed by the sequence data, optionally split over multiple lines. .. note:: Blank or whitespace-only lines are only allowed at the beginning of the file, between FASTA records, or at the end of the file. A blank or whitespace-only line after the header line, within the sequence (for FASTA files), or within quality scores (for QUAL files) will raise an error. scikit-bio will ignore leading and trailing whitespace characters on each line while reading. .. note:: scikit-bio does not currently support legacy FASTA format (i.e., headers/comments denoted with a semicolon). The format supported by scikit-bio (described below in detail) most closely resembles the description given in NCBI's BLAST documentation [3]_. See [2]_ for more details on legacy FASTA format. If you would like legacy FASTA format support added to scikit-bio, please consider submitting a feature request on the `scikit-bio issue tracker <https://github.com/biocore/scikit-bio/issues>`_ (pull requests are also welcome!). Sequence Header ~~~~~~~~~~~~~~~ Each sequence header consists of a single line beginning with a greater-than (``>``) symbol. Immediately following this is a sequence identifier (ID) and description separated by one or more whitespace characters. The sequence ID and description are stored in the sequence `metadata` attribute, under the `'id'` and `'description'` keys, repectively. Both are optional. Each will be represented as the empty string (``''``) in `metadata` if it is not present in the header. A sequence ID consists of a single *word*: all characters after the greater- than symbol and before the first whitespace character (if any) are taken as the sequence ID. Unique sequence IDs are not strictly enforced by the FASTA format itself. A single standardized ID format is similarly not enforced by the FASTA format, though it is often common to use a unique library accession number for a sequence ID (e.g., NCBI's FASTA defline format [5]_). .. note:: scikit-bio will enforce sequence ID uniqueness depending on the type of object that the FASTA file is read into. For example, reading a FASTA file as a generator of ``Sequence`` objects will not enforce unique IDs since it simply yields each sequence it finds in the FASTA file. However, if the FASTA file is read into a ``SequenceCollection`` object, ID uniqueness will be enforced because that is a requirement of a ``SequenceCollection``. If a description is present, it is taken as the remaining characters that follow the sequence ID and initial whitespace(s). The description is considered additional information about the sequence (e.g., comments about the source of the sequence or the molecule that it encodes). For example, consider the following header:: >seq1 db-accession-149855 ``seq1`` is the sequence ID and ``db-accession-149855`` is the sequence description. .. note:: scikit-bio's readers will remove all leading and trailing whitespace from the description. If a header line begins with whitespace following the ``>``, the ID is assumed to be missing and the remainder of the line is taken as the description. Sequence Data ~~~~~~~~~~~~~ Biological sequence data follows the header, and can be split over multiple lines. The sequence data (i.e., nucleotides or amino acids) are stored using the standard IUPAC lexicon (single-letter codes). .. note:: scikit-bio supports both upper and lower case characters. This functionality depends on the type of object the data is being read into. For ``Sequence`` objects, sciki-bio doesn't care about the case. However, for other object types, such as :class:`skbio.sequence.DNA`, :class:`skbio.sequence.RNA`, and :class:`skbio.sequence.Protein`, the `lowercase` parameter must be used to control case functionality. Refer to the documentation for the constructors for details. .. note:: Both ``-`` and ``.`` are supported as gap characters. See :mod:`skbio.sequence` for more details on how scikit-bio interprets sequence data in its in-memory objects. Validation is performed for all scikit-bio objects which support it. This consists of all objects which enforce usage of IUPAC characters. If any invalid IUPAC characters are found in the sequence while reading from the FASTA file, an exception is raised. QUAL Format ^^^^^^^^^^^ A QUAL file contains quality scores for one or more biological sequences stored in a corresponding FASTA file. QUAL format is very similar to FASTA format: it stores records sequentially, with each record beginning with a header line containing a sequence ID and description. The same rules apply to QUAL headers as FASTA headers (see the above sections for details). scikit-bio processes FASTA and QUAL headers in exactly the same way. Quality scores are automatically stored in the object's `positional_metadata` attribute, under the `'quality'` column. Instead of storing biological sequence data in each record, a QUAL file stores a Phred quality score for each base in the corresponding sequence. Quality scores are represented as nonnegative integers separated by whitespace (typically a single space or newline), and can span multiple lines. .. note:: When reading FASTA and QUAL files, scikit-bio requires records to be in the same order in both files (i.e., each FASTA and QUAL record must have the same ID and description after being parsed). In addition to having the same order, the number of FASTA records must match the number of QUAL records (i.e., missing or additonal records are not allowed). scikit-bio also requires that the number of quality scores match the number of bases in the corresponding sequence. When writing FASTA and QUAL files, scikit-bio will maintain the same ordering of records in both files (i.e., using the same ID and description in both records) to support future reading. Format Parameters ----------------- The following parameters are available to change how FASTA/QUAL files are read or written in scikit-bio. QUAL File Parameter (Readers and Writers) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The ``qual`` parameter is available to all FASTA format readers and writers. It can be any file-like type supported by scikit-bio's I/O registry (e.g., file handle, file path, etc.). If ``qual`` is provided when reading, quality scores will be included in each in-memory ``Sequence`` object, in addition to sequence data stored in the FASTA file. When writing, quality scores will be written in QUAL format in addition to the sequence data being written in FASTA format. Reader-specific Parameters ^^^^^^^^^^^^^^^^^^^^^^^^^^ The available reader parameters differ depending on which reader is used. Generator, SequenceCollection, and Alignment Reader Parameters ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The ``constructor`` parameter can be used with the ``Sequence`` generator, ``SequenceCollection``, and ``Alignment`` FASTA readers. ``constructor`` specifies the in-memory type of each sequence that is parsed, and defaults to ``Sequence``. ``constructor`` should be a subclass of ``Sequence``. For example, if you know that the FASTA file you're reading contains protein sequences, you would pass ``constructor=Protein`` to the reader call. .. note:: The FASTA sniffer will not attempt to guess the ``constructor`` parameter, so it will always default to ``Sequence`` if another type is not provided to the reader. Sequence Reader Parameters ~~~~~~~~~~~~~~~~~~~~~~~~~~ The ``seq_num`` parameter can be used with the ``Sequence``, ``DNA``, ``RNA``, and ``Protein`` FASTA readers. ``seq_num`` specifies which sequence to read from the FASTA file (and optional QUAL file), and defaults to 1 (i.e., such that the first sequence is read). For example, to read the 50th sequence from a FASTA file, you would pass ``seq_num=50`` to the reader call. Writer-specific Parameters ^^^^^^^^^^^^^^^^^^^^^^^^^^ The following parameters are available to all FASTA format writers: - ``id_whitespace_replacement``: string to replace **each** whitespace character in a sequence ID. This parameter is useful for cases where an in-memory sequence ID contains whitespace, which would result in an on-disk representation that would not be read back into memory as the same ID (since IDs in FASTA format cannot contain whitespace). Defaults to ``_``. If ``None``, no whitespace replacement is performed and IDs are written as they are stored in memory (this has the potential to create an invalid FASTA-formatted file; see note below). This parameter also applies to a QUAL file if one is provided. - ``description_newline_replacement``: string to replace **each** newline character in a sequence description. Since a FASTA header must be a single line, newlines are not allowed in sequence descriptions and must be replaced in order to write a valid FASTA file. Defaults to a single space. If ``None``, no newline replacement is performed and descriptions are written as they are stored in memory (this has the potential to create an invalid FASTA-formatted file; see note below). This parameter also applies to a QUAL file if one is provided. - ``max_width``: integer specifying the maximum line width (i.e., number of characters) for sequence data and/or quality scores. If a sequence or its quality scores are longer than ``max_width``, it will be split across multiple lines, each with a maximum width of ``max_width``. Note that there are some caveats when splitting quality scores. A single quality score will *never* be split across multiple lines, otherwise it would become two different quality scores when read again. Thus, splitting only occurs *between* quality scores. This makes it possible to have a single long quality score written on its own line that exceeds ``max_width``. For example, the quality score ``12345`` would not be split across multiple lines even if ``max_width=3``. Thus, a 5-character line would be written. Default behavior is to not split sequence data or quality scores across multiple lines. - ``lowercase``: String or boolean array. If a string, it is treated as a key into the positional metadata of the object. If a boolean array, it indicates characters to write in lowercase. Characters in the sequence corresponding to `True` values will be written in lowercase. The boolean array must be the same length as the sequence. .. note:: The FASTA format writers will have noticeably better runtime performance if ``id_whitespace_replacement`` and/or ``description_newline_replacement`` are set to ``None`` so that whitespace replacement is not performed during writing. However, this can potentially create invalid FASTA files, especially if there are newline characters in the IDs or descriptions. For IDs with whitespace, this can also affect how the IDs are read into memory in a subsequent read operation. For example, if an in-memory sequence ID is ``'seq 1'`` and ``id_whitespace_replacement=None``, reading the FASTA file back into memory would result in an ID of ``'seq'``, and ``'1'`` would be part of the sequence description. Examples -------- Reading and Writing FASTA Files ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Suppose we have the following FASTA file with five equal-length sequences (example modified from [6]_):: >seq1 Turkey AAGCTNGGGCATTTCAGGGTGAGCCCGGGCAATACAGGGTAT >seq2 Salmo gair AAGCCTTGGCAGTGCAGGGTGAGCCGTGG CCGGGCACGGTAT >seq3 H. Sapiens ACCGGTTGGCCGTTCAGGGTACAGGTTGGCCGTTCAGGGTAA >seq4 Chimp AAACCCTTGCCG TTACGCTTAAAC CGAGGCCGGGAC ACTCAT >seq5 Gorilla AAACCCTTGCCGGTACGCTTAAACCATTGCCGGTACGCTTAA .. note:: Original copyright notice for the above example file: *(c) Copyright 1986-2008 by The University of Washington. Written by NAME Felsenstein. Permission is granted to copy this document provided that no fee is charged for it and that this copyright notice is not removed.* Note that the sequences are not required to be of equal length in order for the file to be a valid FASTA file (this depends on the object that you're reading the file into). Also note that some of the sequences occur on a single line, while others are split across multiple lines. Let's define this file in-memory as a ``StringIO``, though this could be a real file path, file handle, or anything that's supported by scikit-bio's I/O registry in practice: >>> fl = [u">seq1 Turkey\\n", ... u"AAGCTNGGGCATTTCAGGGTGAGCCCGGGCAATACAGGGTAT\\n", ... u">seq2 Salmo gair\\n", ... u"AAGCCTTGGCAGTGCAGGGTGAGCCGTGG\\n", ... u"CCGGGCACGGTAT\\n", ... u">seq3 H. Sapiens\\n", ... u"ACCGGTTGGCCGTTCAGGGTACAGGTTGGCCGTTCAGGGTAA\\n", ... u">seq4 Chimp\\n", ... u"AAACCCTTGCCG\\n", ... u"TTACGCTTAAAC\\n", ... u"CGAGGCCGGGAC\\n", ... u"ACTCAT\\n", ... u">seq5 Gorilla\\n", ... u"AAACCCTTGCCGGTACGCTTAAACCATTGCCGGTACGCTTAA\\n"] Let's read the FASTA file into a ``SequenceCollection``: >>> from skbio import SequenceCollection >>> sc = SequenceCollection.read(fl) >>> sc.sequence_lengths() [42, 42, 42, 42, 42] >>> sc.ids() [u'seq1', u'seq2', u'seq3', u'seq4', u'seq5'] We see that all 5 sequences have 42 characters, and that each of the sequence IDs were successfully read into memory. Since these sequences are of equal length (presumably because they've been aligned), let's load the FASTA file into an ``Alignment`` object, which is a more appropriate data structure: >>> from skbio import Alignment >>> aln = Alignment.read(fl) >>> aln.sequence_length() 42 Note that we were able to read the FASTA file into two different data structures (``SequenceCollection`` and ``Alignment``) using the exact same ``read`` method call (and underlying reading/parsing logic). Also note that we didn't specify a file format in the ``read`` call. The FASTA sniffer detected the correct file format for us! Let's inspect the type of sequences stored in the ``Alignment``: >>> aln[0] Sequence ------------------------------------------------ Metadata: u'description': u'Turkey' u'id': u'seq1' Stats: length: 42 ------------------------------------------------ 0 AAGCTNGGGC ATTTCAGGGT GAGCCCGGGC AATACAGGGT AT By default, sequences are loaded as ``Sequence`` objects. We can change the type of sequence via the ``constructor`` parameter: >>> from skbio import DNA >>> aln = Alignment.read(fl, constructor=DNA) >>> aln[0] # doctest: +NORMALIZE_WHITESPACE DNA ------------------------------------------------ Metadata: u'description': u'Turkey' u'id': u'seq1' Stats: length: 42 has gaps: False has degenerates: True has non-degenerates: True GC-content: 54.76% ------------------------------------------------ 0 AAGCTNGGGC ATTTCAGGGT GAGCCCGGGC AATACAGGGT AT We now have an ``Alignment`` of ``DNA`` objects instead of ``Sequence`` objects. To write the alignment in FASTA format: >>> from io import StringIO >>> with StringIO() as fh: ... print(aln.write(fh).getvalue()) >seq1 Turkey AAGCTNGGGCATTTCAGGGTGAGCCCGGGCAATACAGGGTAT >seq2 Salmo gair AAGCCTTGGCAGTGCAGGGTGAGCCGTGGCCGGGCACGGTAT >seq3 H. Sapiens ACCGGTTGGCCGTTCAGGGTACAGGTTGGCCGTTCAGGGTAA >seq4 Chimp AAACCCTTGCCGTTACGCTTAAACCGAGGCCGGGACACTCAT >seq5 Gorilla AAACCCTTGCCGGTACGCTTAAACCATTGCCGGTACGCTTAA <BLANKLINE> Both ``SequenceCollection`` and ``Alignment`` load all of the sequences from the FASTA file into memory at once. If the FASTA file is large (which is often the case), this may be infeasible if you don't have enough memory. To work around this issue, you can stream the sequences using scikit-bio's generator-based FASTA reader and writer. The generator-based reader yields ``Sequence`` objects (or subclasses if ``constructor`` is supplied) one at a time, instead of loading all sequences into memory. For example, let's use the generator-based reader to process a single sequence at a time in a ``for`` loop: >>> import skbio.io >>> for seq in skbio.io.read(fl, format='fasta'): ... seq ... print('') Sequence ------------------------------------------------ Metadata: u'description': u'Turkey' u'id': u'seq1' Stats: length: 42 ------------------------------------------------ 0 AAGCTNGGGC ATTTCAGGGT GAGCCCGGGC AATACAGGGT AT <BLANKLINE> Sequence ------------------------------------------------ Metadata: u'description': u'Salmo gair' u'id': u'seq2' Stats: length: 42 ------------------------------------------------ 0 AAGCCTTGGC AGTGCAGGGT GAGCCGTGGC CGGGCACGGT AT <BLANKLINE> Sequence ------------------------------------------------ Metadata: u'description': u'H. Sapiens' u'id': u'seq3' Stats: length: 42 ------------------------------------------------ 0 ACCGGTTGGC CGTTCAGGGT ACAGGTTGGC CGTTCAGGGT AA <BLANKLINE> Sequence ------------------------------------------------ Metadata: u'description': u'Chimp' u'id': u'seq4' Stats: length: 42 ------------------------------------------------ 0 AAACCCTTGC CGTTACGCTT AAACCGAGGC CGGGACACTC AT <BLANKLINE> Sequence ------------------------------------------------ Metadata: u'description': u'Gorilla' u'id': u'seq5' Stats: length: 42 ------------------------------------------------ 0 AAACCCTTGC CGGTACGCTT AAACCATTGC CGGTACGCTT AA <BLANKLINE> A single sequence can also be read into a ``Sequence`` (or subclass): >>> from skbio import Sequence >>> seq = Sequence.read(fl) >>> seq Sequence ------------------------------------------------ Metadata: u'description': u'Turkey' u'id': u'seq1' Stats: length: 42 ------------------------------------------------ 0 AAGCTNGGGC ATTTCAGGGT GAGCCCGGGC AATACAGGGT AT By default, the first sequence in the FASTA file is read. This can be controlled with ``seq_num``. For example, to read the fifth sequence: >>> seq = Sequence.read(fl, seq_num=5) >>> seq Sequence ------------------------------------------------ Metadata: u'description': u'Gorilla' u'id': u'seq5' Stats: length: 42 ------------------------------------------------ 0 AAACCCTTGC CGGTACGCTT AAACCATTGC CGGTACGCTT AA We can use the same API to read the fifth sequence into a ``DNA``: >>> dna_seq = DNA.read(fl, seq_num=5) >>> dna_seq DNA ------------------------------------------------ Metadata: u'description': u'Gorilla' u'id': u'seq5' Stats: length: 42 has gaps: False has degenerates: False has non-degenerates: True GC-content: 50.00% ------------------------------------------------ 0 AAACCCTTGC CGGTACGCTT AAACCATTGC CGGTACGCTT AA Individual sequence objects can also be written in FASTA format: >>> with StringIO() as fh: ... print(dna_seq.write(fh).getvalue()) >seq5 Gorilla AAACCCTTGCCGGTACGCTTAAACCATTGCCGGTACGCTTAA <BLANKLINE> Reading and Writing FASTA/QUAL Files ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ In addition to reading and writing standalone FASTA files, scikit-bio also supports reading and writing FASTA and QUAL files together. Suppose we have the following FASTA file:: >seq1 db-accession-149855 CGATGTC >seq2 db-accession-34989 CATCG Also suppose we have the following QUAL file:: >seq1 db-accession-149855 40 39 39 4 50 1 100 >seq2 db-accession-34989 3 3 10 42 80 >>> fasta_fl = [ ... u">seq1 db-accession-149855\\n", ... u"CGATGTC\\n", ... u">seq2 db-accession-34989\\n", ... u"CATCG\\n"] >>> qual_fl = [ ... u">seq1 db-accession-149855\\n", ... u"40 39 39 4\\n", ... u"50 1 100\\n", ... u">seq2 db-accession-34989\\n", ... u"3 3 10 42 80\\n"] To read in a single ``Sequence`` at a time, we can use the generator-based reader as we did above, providing both FASTA and QUAL files: >>> for seq in skbio.io.read(fasta_fl, qual=qual_fl, format='fasta'): ... seq ... print('') Sequence ------------------------------------------ Metadata: u'description': u'db-accession-149855' u'id': u'seq1' Positional metadata: u'quality': <dtype: uint8> Stats: length: 7 ------------------------------------------ 0 CGATGTC <BLANKLINE> Sequence ----------------------------------------- Metadata: u'description': u'db-accession-34989' u'id': u'seq2' Positional metadata: u'quality': <dtype: uint8> Stats: length: 5 ----------------------------------------- 0 CATCG <BLANKLINE> Note that the sequence objects have quality scores stored as positional metadata since we provided a QUAL file. The other FASTA readers operate in a similar manner. Now let's load the sequences and their quality scores into a ``SequenceCollection``: >>> sc = SequenceCollection.read(fasta_fl, qual=qual_fl) >>> sc <SequenceCollection: n=2; mean +/- std length=6.00 +/- 1.00> To write the sequence data and quality scores in the ``SequenceCollection`` to FASTA and QUAL files, respectively, we run: >>> new_fasta_fh = StringIO() >>> new_qual_fh = StringIO() >>> _ = sc.write(new_fasta_fh, qual=new_qual_fh) >>> print(new_fasta_fh.getvalue()) >seq1 db-accession-149855 CGATGTC >seq2 db-accession-34989 CATCG <BLANKLINE> >>> print(new_qual_fh.getvalue()) >seq1 db-accession-149855 40 39 39 4 50 1 100 >seq2 db-accession-34989 3 3 10 42 80 <BLANKLINE> >>> new_fasta_fh.close() >>> new_qual_fh.close() References ---------- .. [1] NAME NAME (1985). "Rapid and sensitive protein similarity searches". Science 227 (4693): 1435-41. .. [2] http://en.wikipedia.org/wiki/FASTA_format .. [3] http://blast.ncbi.nlm.nih.gov/blastcgihelp.shtml .. [4] https://www.broadinstitute.org/crd/wiki/index.php/Qual .. [5] NAME The BLAST Sequence Analysis Tool. 2002 Oct 9 [Updated 2003 Aug 13]. In: NAME J, NAME J, editors. The NCBI Handbook [Internet]. Bethesda (MD): National Center for Biotechnology Information (US); 2002-. Chapter 16. Available from: http://www.ncbi.nlm.nih.gov/books/NBK21097/ .. [6] http://evolution.genetics.washington.edu/phylip/doc/sequence.html """
#!/usr/bin/env python # (c) 2013, NAME <EMAIL> # # This file is part of Ansible. # # Ansible is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Ansible is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Ansible. If not, see <http://www.gnu.org/licenses/>. # # # Author: NAME <EMAIL> # # Description: # This module queries local or remote Docker daemons and generates # inventory information. # # This plugin does not support targeting of specific hosts using the --host # flag. Instead, it queries the Docker API for each container, running # or not, and returns this data all once. # # The plugin returns the following custom attributes on Docker containers: # docker_args # docker_config # docker_created # docker_driver # docker_exec_driver # docker_host_config # docker_hostname_path # docker_hosts_path # docker_id # docker_image # docker_name # docker_network_settings # docker_path # docker_resolv_conf_path # docker_state # docker_volumes # docker_volumes_rw # # Requirements: # The docker-py module: https://github.com/dotcloud/docker-py # # Notes: # A config file can be used to configure this inventory module, and there # are several environment variables that can be set to modify the behavior # of the plugin at runtime: # DOCKER_CONFIG_FILE # DOCKER_HOST # DOCKER_VERSION # DOCKER_TIMEOUT # DOCKER_PRIVATE_SSH_PORT # DOCKER_DEFAULT_IP # # Environment Variables: # environment variable: DOCKER_CONFIG_FILE # description: # - A path to a Docker inventory hosts/defaults file in YAML format # - A sample file has been provided, colocated with the inventory # file called 'docker.yml' # required: false # default: Uses docker.docker.Client constructor defaults # environment variable: DOCKER_HOST # description: # - The socket on which to connect to a Docker daemon API # required: false # default: Uses docker.docker.Client constructor defaults # environment variable: DOCKER_VERSION # description: # - Version of the Docker API to use # default: Uses docker.docker.Client constructor defaults # required: false # environment variable: DOCKER_TIMEOUT # description: # - Timeout in seconds for connections to Docker daemon API # default: Uses docker.docker.Client constructor defaults # required: false # environment variable: DOCKER_PRIVATE_SSH_PORT # description: # - The private port (container port) on which SSH is listening # for connections # default: 22 # required: false # environment variable: DOCKER_DEFAULT_IP # description: # - This environment variable overrides the container SSH connection # IP address (aka, 'ansible_ssh_host') # # This option allows one to override the ansible_ssh_host whenever # Docker has exercised its default behavior of binding private ports # to all interfaces of the Docker host. This behavior, when dealing # with remote Docker hosts, does not allow Ansible to determine # a proper host IP address on which to connect via SSH to containers. # By default, this inventory module assumes all IP_ADDRESS-exposed # ports to be bound to localhost:<port>. To override this # behavior, for example, to bind a container's SSH port to the public # interface of its host, one must manually set this IP. # # It is preferable to begin to launch Docker containers with # ports exposed on publicly accessible IP addresses, particularly # if the containers are to be targeted by Ansible for remote # configuration, not accessible via localhost SSH connections. # # Docker containers can be explicitly exposed on IP addresses by # a) starting the daemon with the --ip argument # b) running containers with the -P/--publish ip::containerPort # argument # default: IP_ADDRESS if port exposed on IP_ADDRESS by Docker # required: false # # Examples: # Use the config file: # DOCKER_CONFIG_FILE=./docker.yml docker.py --list # # Connect to docker instance on localhost port 4243 # DOCKER_HOST=tcp://localhost:4243 docker.py --list # # Any container's ssh port exposed on IP_ADDRESS will mapped to # another IP address (where Ansible will attempt to connect via SSH) # DOCKER_DEFAULT_IP=IP_ADDRESS docker.py --list
#!/usr/bin/env python # ***** BEGIN LICENSE BLOCK ***** # Version: MPL 1.1/GPL 2.0/LGPL 2.1 # # The contents of this file are subject to the Mozilla Public License Version # 1.1 (the "License"); you may not use this file except in compliance with # the License. You may obtain a copy of the License at # http://www.mozilla.org/MPL/ # # Software distributed under the License is distributed on an "AS IS" basis, # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License # for the specific language governing rights and limitations under the # License. # # The Original Code is font utility code. # # The Initial Developer of the Original Code is Mozilla Corporation. # Portions created by the Initial Developer are Copyright (C) 2009 # the Initial Developer. All Rights Reserved. # # Contributor(s): # NAME <EMAIL> # # Alternatively, the contents of this file may be used under the terms of # either the GNU General Public License Version 2 or later (the "GPL"), or # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), # in which case the provisions of the GPL or the LGPL are applicable instead # of those above. If you wish to allow use of your version of this file only # under the terms of either the GPL or the LGPL, and not to allow others to # use your version of this file under the terms of the MPL, indicate your # decision by deleting the provisions above and replace them with the notice # and other provisions required by the GPL or the LGPL. If you do not delete # the provisions above, a recipient may use your version of this file under # the terms of any one of the MPL, the GPL or the LGPL. # # ***** END LICENSE BLOCK ***** */ # eotlitetool.py - create EOT version of OpenType font for use with IE # # Usage: eotlitetool.py [-o output-filename] font1 [font2 ...] # # OpenType file structure # http://www.microsoft.com/typography/otspec/otff.htm # # Types: # # BYTE 8-bit unsigned integer. # CHAR 8-bit signed integer. # USHORT 16-bit unsigned integer. # SHORT 16-bit signed integer. # ULONG 32-bit unsigned integer. # Fixed 32-bit signed fixed-point number (16.16) # LONGDATETIME Date represented in number of seconds since 12:00 midnight, January 1, 1904. The value is represented as a signed 64-bit integer. # # SFNT Header # # Fixed sfnt version // 0x00010000 for version 1.0. # USHORT numTables // Number of tables. # USHORT searchRange // (Maximum power of 2 <= numTables) x 16. # USHORT entrySelector // Log2(maximum power of 2 <= numTables). # USHORT rangeShift // NumTables x 16-searchRange. # # Table Directory # # ULONG tag // 4-byte identifier. # ULONG checkSum // CheckSum for this table. # ULONG offset // Offset from beginning of TrueType font file. # ULONG length // Length of this table. # # OS/2 Table (Version 4) # # USHORT version // 0x0004 # SHORT xAvgCharWidth # USHORT usWeightClass # USHORT usWidthClass # USHORT fsType # SHORT ySubscriptXSize # SHORT ySubscriptYSize # SHORT ySubscriptXOffset # SHORT ySubscriptYOffset # SHORT ySuperscriptXSize # SHORT ySuperscriptYSize # SHORT ySuperscriptXOffset # SHORT ySuperscriptYOffset # SHORT yStrikeoutSize # SHORT yStrikeoutPosition # SHORT sFamilyClass # BYTE panose[10] # ULONG ulUnicodeRange1 // Bits 0-31 # ULONG ulUnicodeRange2 // Bits 32-63 # ULONG ulUnicodeRange3 // Bits 64-95 # ULONG ulUnicodeRange4 // Bits 96-127 # CHAR achVendID[4] # USHORT fsSelection # USHORT usFirstCharIndex # USHORT usLastCharIndex # SHORT sTypoAscender # SHORT sTypoDescender # SHORT sTypoLineGap # USHORT usWinAscent # USHORT usWinDescent # ULONG ulCodePageRange1 // Bits 0-31 # ULONG ulCodePageRange2 // Bits 32-63 # SHORT sxHeight # SHORT sCapHeight # USHORT usDefaultChar # USHORT usBreakChar # USHORT usMaxContext # # # The Naming Table is organized as follows: # # [name table header] # [name records] # [string data] # # Name Table Header # # USHORT format // Format selector (=0). # USHORT count // Number of name records. # USHORT stringOffset // Offset to start of string storage (from start of table). # # Name Record # # USHORT platformID // Platform ID. # USHORT encodingID // Platform-specific encoding ID. # USHORT languageID // Language ID. # USHORT nameID // Name ID. # USHORT length // String length (in bytes). # USHORT offset // String offset from start of storage area (in bytes). # # head Table # # Fixed tableVersion // Table version number 0x00010000 for version 1.0. # Fixed fontRevision // Set by font manufacturer. # ULONG checkSumAdjustment // To compute: set it to 0, sum the entire font as ULONG, then store 0xB1B0AFBA - sum. # ULONG magicNumber // Set to 0x5F0F3CF5. # USHORT flags # USHORT unitsPerEm // Valid range is from 16 to 16384. This value should be a power of 2 for fonts that have TrueType outlines. # LONGDATETIME created // Number of seconds since 12:00 midnight, January 1, 1904. 64-bit integer # LONGDATETIME modified // Number of seconds since 12:00 midnight, January 1, 1904. 64-bit integer # SHORT xMin // For all glyph bounding boxes. # SHORT yMin # SHORT xMax # SHORT yMax # USHORT macStyle # USHORT lowestRecPPEM // Smallest readable size in pixels. # SHORT fontDirectionHint # SHORT indexToLocFormat // 0 for short offsets, 1 for long. # SHORT glyphDataFormat // 0 for current format. # # # # Embedded OpenType (EOT) file format # http://www.w3.org/Submission/EOT/ # # EOT version 0x00020001 # # An EOT font consists of a header with the original OpenType font # appended at the end. Most of the data in the EOT header is simply a # copy of data from specific tables within the font data. The exceptions # are the 'Flags' field and the root string name field. The root string # is a set of names indicating domains for which the font data can be # used. A null root string implies the font data can be used anywhere. # The EOT header is in little-endian byte order but the font data remains # in big-endian order as specified by the OpenType spec. # # Overall structure: # # [EOT header] # [EOT name records] # [font data] # # EOT header # # ULONG eotSize // Total structure length in bytes (including string and font data) # ULONG fontDataSize // Length of the OpenType font (FontData) in bytes # ULONG version // Version number of this format - 0x00020001 # ULONG flags // Processing Flags (0 == no special processing) # BYTE fontPANOSE[10] // OS/2 Table panose # BYTE charset // DEFAULT_CHARSET (0x01) # BYTE italic // 0x01 if ITALIC in OS/2 Table fsSelection is set, 0 otherwise # ULONG weight // OS/2 Table usWeightClass # USHORT fsType // OS/2 Table fsType (specifies embedding permission flags) # USHORT magicNumber // Magic number for EOT file - 0x504C. # ULONG unicodeRange1 // OS/2 Table ulUnicodeRange1 # ULONG unicodeRange2 // OS/2 Table ulUnicodeRange2 # ULONG unicodeRange3 // OS/2 Table ulUnicodeRange3 # ULONG unicodeRange4 // OS/2 Table ulUnicodeRange4 # ULONG codePageRange1 // OS/2 Table ulCodePageRange1 # ULONG codePageRange2 // OS/2 Table ulCodePageRange2 # ULONG checkSumAdjustment // head Table CheckSumAdjustment # ULONG reserved[4] // Reserved - must be 0 # USHORT padding1 // Padding - must be 0 # # EOT name records # # USHORT FamilyNameSize // Font family name size in bytes # BYTE FamilyName[FamilyNameSize] // Font family name (name ID = 1), little-endian UTF-16 # USHORT Padding2 // Padding - must be 0 # # USHORT StyleNameSize // Style name size in bytes # BYTE StyleName[StyleNameSize] // Style name (name ID = 2), little-endian UTF-16 # USHORT Padding3 // Padding - must be 0 # # USHORT VersionNameSize // Version name size in bytes # bytes VersionName[VersionNameSize] // Version name (name ID = 5), little-endian UTF-16 # USHORT Padding4 // Padding - must be 0 # # USHORT FullNameSize // Full name size in bytes # BYTE FullName[FullNameSize] // Full name (name ID = 4), little-endian UTF-16 # USHORT Padding5 // Padding - must be 0 # # USHORT RootStringSize // Root string size in bytes # BYTE RootString[RootStringSize] // Root string, little-endian UTF-16