notes

notes

vlq

REGULARS

([abc]) - creates a group that contains a set for the letters 'a', 'b', and 'c'. This could be later accessed from the Match object as .group(1)

(?P<name>[abc]) - creates a named group that contains a set for the letters 'a', 'b', and 'c'. This could later be accessed from the Match object as .group('name').

.groups() - method to show all of the groups on a Match object.

re.MULTILINE or re.M - flag to make a pattern regard lines in your text as the beginning or end of a string.

^ - specifies, in a pattern, the beginning of the string.

$ - specifies, in a pattern, the end of the string.
^(?!.*SEARCH_STRING).*$ - example with negative look-around for "SEARCH_STRING"


LUTZ

map example:

m = [[1,2,3],[4,5,6],[7,8,9]]

print(list(map(sum,m)))  

comprehensions:

[ord(x) for x in 'spaam'] # List of character ordinals   >>> [115, 112, 97, 97, 109]

{ord(x) for x in 'spaam'} # Sets remove duplicates     >>> {112, 97, 115, 109}

{x: ord(x) for x in 'spaam'} # Dictionary keys are unique >>> {'p': 112, 'a': 97, 's': 115, 'm': 109}

(ord(x) for x in 'spaam') # Generator of values      >>> <generator object <genexpr> at 0x000000000254DAB0>

stripping

calling the line.rstrip method is often preferred for stripping newline

characters because this call leaves the line intact if it has no newline character at the

end—a common case for files created with some text-editing tools. Slicing works if

you’re sure the line is properly terminated.

string formatting

'%(qty)d more %(food)s' % {'qty': 1, 'food': 'spam'}

This trick is also used in conjunction with the vars built-in function, which returns a

dictionary containing all the variables that exist in the place it is called:

food = 'spam'

qty = 10

vars()

#{'food': 'spam', 'qty': 10, ...plus built-in names set by Python... }

'My {map[kind]} runs {sys.platform}'.format(sys=sys, map={'kind': 'laptop'})

eval

['[1, 2, 3]', "{'a': 1, 'b': 2}\n"] 

>>> eval(parts[0])

[1, 2, 3]

multi-line assignment

[spam, ham] = ['yum', 'YUM']

a, b, c, d = 'spam'

a, *b = 'spam'

dict

dict.get('value', value_on_fail)


Report Page