background preloader

Python

Facebook Twitter

26.2. pdb — The Python Debugger. Source code: Lib/pdb.py The module pdb defines an interactive source code debugger for Python programs. It supports setting (conditional) breakpoints and single stepping at the source line level, inspection of stack frames, source code listing, and evaluation of arbitrary Python code in the context of any stack frame. It also supports post-mortem debugging and can be called under program control. The debugger is extensible — it is actually defined as the class Pdb. This is currently undocumented but easily understood by reading the source. The debugger’s prompt is (Pdb). >>> import pdb>>> import mymodule>>> pdb.run('mymodule.test()')><string>(0)? Pdb.py can also be invoked as a script to debug other scripts.

Python -m pdb myscript.py When invoked as a script, pdb will automatically enter post-mortem debugging if the program being debugged exits abnormally. New in version 2.4: Restarting post-mortem behavior added. import pdb; pdb.set_trace() The typical usage to inspect a crashed program is: The Python Tutorial — Python v3.2.3 documentation. Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python’s elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms. The Python interpreter and the extensive standard library are freely available in source or binary form for all major platforms from the Python web site, and may be freely distributed.

The same site also contains distributions of and pointers to many free third party Python modules, programs and tools, and additional documentation. The Python interpreter is easily extended with new functions and data types implemented in C or C++ (or other languages callable from C). Python is also suitable as an extension language for customizable applications. The Glossary is also worth going through. Introduction to unittest. Starting Testing with Python Note In Python 2.7 and 3.2 a whole bunch of improvements to unittest will arrive. The major changes include new assert methods, clean up functions, assertRaises as a context manager, new command line features, test discovery and the load_tests protocol. unittest2 is a backport of the new features (and tests) to work with Python 2.4, 2.5 & 2.6.

See: unittest2: improvements to the unittest module As a dynamic language Python is substantially easier to test than other languages, which means that there is absolutely no excuse for not having good tests for your Python projects. This article is about testing in Python, mainly using the Python standard library testing framework unittest. There are many different ways of categorizing tests, and many names for subtly different styles of testing. Unit testsFunctional tests (black box tests, acceptance tests, integration tests)Regression tests Regression testing checks that bugs you’ve fixed don’t recur. Python discover.py. Python - Dictionary Data Type. A dictionary is mutable and is another container type that can store any number of Python objects, including other container types. Dictionaries consist of pairs (called items) of keys and their corresponding values. Python dictionaries are also known as associative arrays or hash tables.

The general syntax of a dictionary is as follows: You can create dictionary in the following way as well: Each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces. An empty dictionary without any items is written with just two curly braces, like this: {}. Keys are unique within a dictionary while values may not be. Accessing Values in Dictionary: To access dictionary elements, you can use the familiar square brackets along with the key to obtain its value. When the above code is executed, it produces the following result: dict['Name']: Zara dict['Age']: 7 Updating Dictionary: dict['Age']: 8 dict['School']: DPS School.

5. Built-in Types. The following sections describe the standard types that are built into the interpreter. Note Historically (until release 2.2), Python’s built-in types have differed from user-defined types because it was not possible to use the built-in types as the basis for object-oriented inheritance. This limitation no longer exists. The principal built-in types are numerics, sequences, mappings, files, classes, instances and exceptions. Some operations are supported by several object types; in particular, practically all objects can be compared, tested for truth value, and converted to a string (with the repr() function or the slightly different str() function). The latter function is implicitly used when an object is written by the print() function. 5.1. Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below. All other values are considered true — so objects of many types are always true. 5.2.

Notes: 5.3. ! 5.4. 5.4.1. 5.4.2. 5.4.3. 4. More Control Flow Tools. Besides the while statement just introduced, Python knows the usual control flow statements known from other languages, with some twists. 4.1. if Statements Perhaps the most well-known statement type is the if statement. For example: >>> x = int(raw_input("Please enter an integer: "))Please enter an integer: 42>>> if x < 0:... x = 0... print 'Negative changed to zero'... elif x == 0:... print 'Zero'... elif x == 1:... print 'Single'... else:... print 'More'...More There can be zero or more elif parts, and the else part is optional. The keyword ‘elif‘ is short for ‘else if’, and is useful to avoid excessive indentation. 4.2. for Statements The for statement in Python differs a bit from what you may be used to in C or Pascal. >>> # Measure some strings:... words = ['cat', 'window', 'defenestrate']>>> for w in words:... print w, len(w)...cat 3window 6defenestrate 12 4.3.

If you do need to iterate over a sequence of numbers, the built-in function range() comes in handy. 4.5. pass Statements 4.6. Chapter 2. Your First Python Program. 2.5.5. We are pleased to announce the release of Python 2.5.5, a security fix release of Python 2.5, on January 31st, 2010.Python 2.5.5 has been replaced by a newer bugfix release of Python. Please download Python 2.5.6 instead.The last binary release of Python 2.5 was 2.5.4. This is a source-only release that only includes security fixes. The last full bug-fix release of Python 2.5 was Python 2.5.4. User are encouraged to upgrade to the latest release of Python 2.7 (which is 2.7.2 at this point). This releases fixes issues with the logging, tarfile and expat modules, and with thread-local variables.

See also the license. gzip-compressed source code: Python-2.5.5.tgz bzip2-compressed source code: Python-2.5.5.tar.bz2, the source archive. The bzip2-compressed version is considerably smaller, so get that one if your system has the appropriate tools to deal with it. Unpack the archive with tar -zxvf Python-2.5.5.tgz (or bzcat Python-2.5.5.tar.bz2 | tar -xf -). BeginnersGuide/Programmers. This is a Wiki page. Users with edit rights can edit it. You are, therefore, free to (in fact, encouraged to) add details of material that other Python users will find useful.

It is not an advertising page and is here to serve the whole Python community. Users who continually edit pages to give their own materials (particularly commercial materials) prominence, or spam the listing with multiple entries which point to resources with only slightly altered material, may subsequently find their editing rights disabled. You have been warned. A beginner-friendly Python tutorial that starts with the absolute basics but also covers more advanced stuff like Python software deployment.