Python Tips - yszheda/wiki GitHub Wiki

list

search in a list

list comprehension

function

db

ConfigParser

closure

context

getopt

local variable referenced before assignment

assert

tuple

has_key / in

ds/data struction

dictionary

nested

func

setdefault

arg

escape

stderr

function takes exactly 1 argument (2 given)

ssh

rsync

shell

string

trim

split

format

start char

substring

remove str

stringio

regex

multiline

CJK

fast concat / join

join

mail

pprint#

exception

json

class to json

field handle

Dump object fileds

inspect.getmembers(object[, predicate])

img

timestamp

sort

file IO

split

basename

copy

deepcopy

remove file

file ext

ls

dir

mkdir

import os
if not os.path.exists(directory):
    os.makedirs(directory)

os.walk

filecmp

duplicate files

xml

sax

recursive

XPath

ElementTree to String

pretty print xml

empty line

lxml

find xml element

namespace

from lxml import etree

# http://wiki.tei-c.org/index.php/Remove-Namespaces.xsl
xslt = """<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="no"/>

<xsl:template match="/|comment()|processing-instruction()">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

<xsl:template match="*">
    <xsl:element name="{local-name()}">
      <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
</xsl:template>

<xsl:template match="@*">
    <xsl:attribute name="{local-name()}">
      <xsl:value-of select="."/>
    </xsl:attribute>
</xsl:template>
</xsl:stylesheet>
"""

def remove_xml_namespaces(xml_root):
    xslt_doc = etree.parse(io.StringIO(xslt))
    transform = etree.XSLT(xslt_doc)
    return transform(xml_root).getroot()

'lxml.etree._Element' object has no attribute 'write'

from lxml import etree

root = etree.Element('root1') 
element = etree.SubElement(root, 'element1')
tree = root.getroottree()
print(type(tree))
# <type 'lxml.etree._ElementTree'>
tree.write('xmltree.xml') 

expected lxml.etree._Element, got lxml.etree._XSLTResultTree

pep8

vim

git

datetime

call other python

module

TypeError: module.init() takes at most 2 arguments (3 given)

pyc

build binary

py2exe

cx_Freeze

global

photoshop

class

copy fields

subprocess

check error

capture output of commands

excel

isnumber

remove key from dict

copy dict

underscore var

EAPF vs LBYL

snake case 2 camel case

glob

append files

excel

openpyxl

isinstance

unicode

Profile

virtual env

site-packages / dist-packages

Image Diff

Matplot

savefig

subplot

__FILE__ / __LINE__

print(sys._getframe().f_lineno)

C / C++

pybind11

Debug

pdb

python -m pdb myscript.py
(Pdb) import sys                                                                                                                                              
(Pdb) sys.path.append('')
(Pdb) restart
import pdb; pdb.pm()

frame

w(here)
Print a stack trace, with the most recent frame at the bottom. An arrow indicates the current frame, which determines the context of most commands.

d(own) [count]
Move the current frame count (default one) levels down in the stack trace (to a newer frame).

u(p) [count]
Move the current frame count (default one) levels up in the stack trace (to an older frame).

Web

download file

CUDA

wheel package

unzip /path/to/file.egg
unzip /path/to/file.whl

Trouble shooting

Population must be a sequence or set.

[Numpy] warning: ‘_import_array’ defined but not used

relative import

⚠️ **GitHub.com Fallback** ⚠️