Table of Contents

[hemmerling] Python 2/10

Related pages:

Python - The Programming Language

Abstract methods, @classmethod, @staticmethod

Anonymous functions

Decorators - @ -

Duck Typing

if not

Metaclass

Regular Expressions

Example 1 - Extract

import re
filename = "file2016.txt"
match = re.search(r"[0-9{4}",filename)
match.group(0)

Example 2 - Replace

re.sub(r"[0-9{4}","2011",filename)

Resources

*vargs & **kwargs

Tips & Tricks

General

x = [1,2]
y = x
z = [x[0],x[1]]
print x, y, z
y[0] = 3
print x, y, z
[1, 2] [1, 2] [1, 2]
[3, 2] [3, 2] [1, 2]

CodeSculptor

Reflection

Reflection with Python 2.7.3 - Code Example

class Foo:
    def hello(self):
        print "Hello World"

# without reflection
obj = Foo()
obj.hello()

# with reflection
class_name = "Foo"
method = "hello"
obj = globals()[class_name]()
getattr(obj, method)()

Simulation of a Reflection with CodeSculptor - Code Example

global_dict = {}
def globals():
    return global_dict

class Foo:
    def hello(self):
        print "Hello World"

global_dict['Foo'] = Foo

# without reflection
obj = Foo()
obj.hello()

# with reflection
class_name = "Foo"
method = "hello"
obj = globals()[class_name]()
getattr(obj, method)()

Resources

Testing

Text Formatting

Indentation Problem

Error: Inconsistent indentation detected!
1) Your indentation is outright incorrect (easy to fix), OR
2) Your indentation mixes tabs and spaces.
To fix case 2, change all tabs to spaces by using 
Edit->Select All followed by Format->Untabify Region 
and speicfiy the number of columns used by each tab

Indentation Problem

Syntax error
There's an error in your program:
unindent does not match any outer indentation level

Windows Platform

Python File Extensions with Windows

Modules you should not try to install with "easy_install" on Windows, as Rookie :-(

Setting up a Compiler Chain to install Modules which need a working "C" Compiler Chain installed

Calling Pylint from PyScripter on Windows Platform

Command line: C:\Python27\python.exe C:\Python27\Lib\site-packages\pylint\lint.py 
C:\Users\Public\projects\SPOTIF~1\REVERS~1.PY -f parseable

C:\Users\Public\projects\SPOTIF~1\REVERS~1.py:1: 
[C] Invalid name "REVERS~1" for type module
(should match (([a-z_][a-z0-9_]*)|([A-Z][a-zA-Z0-9]+))$)

Process "Pylint" terminated, ExitCode: 00000010

List of all installed Python Modules

AttributeError: 'module' object has no attribute '__version__'

Appropriate OpenDirectory Directory Pages


When this document changes ! Site Navigation ( My Business ! My Topics ! Imprint / Contact ! Privacy Policy ! Keyword Index ! ! Google+ Publisher "hemmerling" )