\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{Marcos} \pdfinfo{ /Title (marcos-gral.pdf) /Creator (Cheatography) /Author (Marcos) /Subject (marcos-gral Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{F25288} \definecolor{LightBackground}{HTML}{FEF4F7} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{marcos-gral Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Marcos} via \textcolor{DarkBackground}{\uline{cheatography.com/34357/cs/16484/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}Marcos \\ \uline{cheatography.com/marcos} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 29th July, 2018.\\ Updated 29th July, 2018.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{2} \begin{tabularx}{8.4cm}{x{3.92 cm} x{4.08 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Python Classes}} \tn % Row 0 \SetRowColor{LightBackground} class Child(Parent): & def \_\_init\_\_(self, {\emph{args, }}*kwargs): \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{super().\_\_init\_\_(self}, args) \# ??? & @staticmethod \# ????? \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{@classmethod \# ?????} \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} @property \# for getter & @property.setter \tn % Row Count 7 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Python}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{lambda {[}parameter\_list{]}: expresion \newline \newline def func(n): \newline """ Documentation """} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{2.08 cm} x{5.92 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Python Indexes and Slices}} \tn % Row 0 \SetRowColor{LightBackground} len(a) & a{[}::-1{]} \# reverse \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{b=a{[}:{]} \# Shallow copy} \tn % Row Count 2 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4.08 cm} x{3.92 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Python Lists}} \tn % Row 0 \SetRowColor{LightBackground} lst.append(item) & lst.pop(item) \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} lst.count(item) & lst.remove(item) \# first item found \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{del lst{[}i{]}} \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} lst.extend(lst2) & lst.reverse() \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} lst.index(item) & lst.sort() \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} sorted(ll) \# sorts without modifying & \seqsplit{lst.insert(position}, item) \tn % Row Count 8 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{[}x for x in lst if cond{]}} \tn % Row Count 9 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4.16 cm} x{3.84 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Dictionary Operations}} \tn % Row 0 \SetRowColor{LightBackground} len(d) & del d{[}key{]} \# KeyError Exception \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} k in d & d.keys() \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} d.setdefault(key{[},default{]}) \# ??? & d.clear() \# ??? \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} dict(a=1, b=2) & dict(zip({[}'a', 'b'{]}, {[}1, 2{]})) \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} dict({[}('A', 1), ('Z', -1){]}) & dict(\{'Z': -1, 'A': 1\}) \tn % Row Count 9 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.84 cm} x{6.16 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Comprehensions}} \tn % Row 0 \SetRowColor{LightBackground} list & {[} expression for iterable\_clause if optional\_filter{]} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} nested lists & {[} expression for iterable\_clause\_1 for iterable\_clause\_2 if optional\_filter{]} \tn % Row Count 7 (+ 4) % Row 2 \SetRowColor{LightBackground} dict & \{ key\_expression: value\_expression for iterable\_clause if optional\_filter\} \tn % Row Count 11 (+ 4) % Row 3 \SetRowColor{white} set & \{ value\_expression for iterable\_clause if optional\_filter\} \tn % Row Count 14 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.16 cm} x{5.84 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Python Scripting}} \tn % Row 0 \SetRowColor{LightBackground} sys.argv & parametes pass to the command \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \seqsplit{sys.exit(0)} & 0 is success \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} sys.path & list of paths to packages \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{sys.version\_info} \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} \seqsplit{os.environ} & Dictionary of environment Vars \tn % Row Count 7 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{os.curdir} \tn % Row Count 8 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Python requests}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{r = requests.get(url , params=\{'foo': 'bar'\} ) \newline r = requests.post(url , params=\{'foo': 'bar'\} ) \newline \newline r.status\_code \# 200 \newline r.text \newline r.json \newline r.headers \newline r.encoding \# utf-8 / ISO-xxx \newline \newline import json \newline url = \seqsplit{'https://api.github.com/some/endpoint'} \newline payload = \{'some': 'data'\} \newline headers = \{'content-type': 'application/json'\} \newline \newline r = requests.post(url, data=json.dumps(payload), headers=headers) \newline \newline r = \seqsplit{requests.put("http://httpbin.org/put")} \newline r = \seqsplit{requests.delete("http://httpbin.org/delete")} \newline r = \seqsplit{requests.head("http://httpbin.org/get")} \newline r = \seqsplit{requests.options("http://httpbin.org/get")}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.6 cm} x{4.4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Python Datetime}} \tn % Row 0 \SetRowColor{LightBackground} dt.today() & dt.now(timezoneinfo) \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} dt.combine(date, time) & dt.utcnow() \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} dt.strptime(date, format) & \seqsplit{dt.fromtimestamp(timestamp)} \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} dt.timestamp() \# ???? & \seqsplit{dt.utcfromtimestamp(timestamp)} \tn % Row Count 7 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{from datetime import datetime as dt} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Python Time Methods}} \tn % Row 0 \SetRowColor{LightBackground} time.replace() & time.utcoffset() \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} time.isoformat() & time.dst() \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{time.tzname()} \tn % Row Count 3 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{0.8 cm} p{0.8 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Python Date Formatting}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\seqsplit{https://www.cheatography.com/davechild/cheat-sheets/python/}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Python Iteration}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{for i in range(start,stop,step): \newline stuff \newline \newline for value in {[}sequence{]}: \newline stuff \newline \newline for position, value in enumerate(sequence): \newline stuff \newline \newline for a,b in zip(first, second): \newline stuff \newline \newline for \#\#\# \newline else \newline stuff to do at end of loop (usually exception when breaking in loop) \newline \newline while condition: \newline stuff \newline else \newline stuff to do when condition is false \newline \newline break \# breaks the loop \newline continue \# continue at the start of the loop body} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{module itertools provides lots of interesting tools for iteration} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{varios}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{"Hello, \%s." \% name \newline % Row Count 1 (+ 1) "Hello, \%s. You are \%s." \% (name, age) \newline % Row Count 2 (+ 1) "Hello, \{\}. You are \{\}.".format(name, age) \newline % Row Count 3 (+ 1) "Hello, \{1\}. You are \{0\}.".format(age, name) \newline % Row Count 4 (+ 1) person = \{'name': 'Eric', 'age': 74\} \newline % Row Count 5 (+ 1) "Hello, \{name\}. You are \{age\}.".format(name=person{[}'name'{]}, age=person{[}'age'{]}) \newline % Row Count 7 (+ 2) "Hello, \{name\}. You are \{age\}.".format(**person) \newline % Row Count 8 (+ 1) \textgreater{}\textgreater{}\textgreater{} name = "Eric" \newline % Row Count 9 (+ 1) \textgreater{}\textgreater{}\textgreater{} age = 74 \newline % Row Count 10 (+ 1) \textgreater{}\textgreater{}\textgreater{} f"Hello, \{name\}. You are \{age\}." \newline % Row Count 11 (+ 1) F"Hello, \{name\}. You are \{age\}." \newline % Row Count 12 (+ 1) f"\{name.lower()\} is funny." \newline % Row Count 13 (+ 1) f"\{my:func(name)\} is funny." \newline % Row Count 14 (+ 1) \seqsplit{https://realpython.com/python-f-strings/} \newline % Row Count 15 (+ 1) \seqsplit{=============================================}% Row Count 16 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.64 cm} x{5.36 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Operations on Sets ????}} \tn % Row 0 \SetRowColor{LightBackground} | & union \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \& & intersection \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} -\textasciicircum{} & difference/symetric diff \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \textless{} \textless{}= \textgreater{} \textgreater{}= & inclusion relations \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} s.update(s2) & s.add(key) \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} s.copy() & s.discard(key) \tn % Row Count 6 (+ 1) % Row 6 \SetRowColor{LightBackground} s.pop() & s.clear() \tn % Row Count 7 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4.24 cm} x{3.76 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Python Math}} \tn % Row 0 \SetRowColor{LightBackground} 5 // 2 = 2 & 5 \% 2 = 1 \tn % Row Count 1 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.4 cm} x{5.6 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Python Generators}} \tn % Row 0 \SetRowColor{LightBackground} yield x & next(func) \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \seqsplit{generator.send(x)} & for in in \seqsplit{generator\_function(**some\_params)} \tn % Row Count 4 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{yield from list\_comprehension \# ????} \tn % Row Count 5 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4.24 cm} x{3.76 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{in-built functions}} \tn % Row 0 \SetRowColor{LightBackground} min(values), max(values) & range(start, stop{[}, step{]}) \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} filter(function, array) \# ??? & map(func, array) \# ??? \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} id(object) & round(n, {[}decimal places{]} \tn % Row Count 6 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Python Common Exceptions}} \tn % Row 0 \SetRowColor{LightBackground} IndexError & KeyError \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} StopIteration & TimeoutError \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} AttributeError & AssertionError \tn % Row Count 3 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\seqsplit{https://docs.python.org/3/library/exceptions.html}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.76 cm} x{4.24 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Python Random}} \tn % Row 0 \SetRowColor{LightBackground} random.seed(1) & \seqsplit{random.randrange(stop)} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{random.randrange(start}, stop{[},step{]}) & random.randint(a, b) \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \seqsplit{random.choice(seq)} & \seqsplit{random.choices(population}, k=1) \# IndexError \tn % Row Count 7 (+ 3) % Row 3 \SetRowColor{white} random.shuffle(x) & \seqsplit{random.sample(population}, k) \tn % Row Count 9 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.6 cm} x{4.4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Python File}} \tn % Row 0 \SetRowColor{LightBackground} f = open(path) & f.read() \# Read f \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} f.readline() & f.readlines() \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} f.write(s) & f.close() \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{with f = open(path, 'r'):} \tn % Row Count 4 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.52 cm} x{4.48 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Python Regular expressions Module}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{re.compile(pattern}, flags=0) & regex.search(string{[},pos{]}{[},endpos{]}) \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{regex.match(string)} & \seqsplit{regex.fullMatch(string)} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} match.group({[}group1, ...{]}) & match.groups() \tn % Row Count 6 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.76 cm} x{4.24 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Python String Methos}} \tn % Row 0 \SetRowColor{LightBackground} s.lstrinp() & s.partition() \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} s.decode() \# ??? & s.rjust(wirth{[}, fillchar{]}) \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} s.rfind(item) & s.split(sep) \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} s.splitlines() & s.isalpha() \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} s.isdigit() & s.startswith(sub) \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} s.strip() & s.isspace() \tn % Row Count 7 (+ 1) % Row 6 \SetRowColor{LightBackground} s.encode('utf-8') \# ??? & b"string" \# bytes object \tn % Row Count 9 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{String Formating}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{"Hello, \{0\} \{1\}".format("abe", "jones") \newline Hello, abe jones \newline \newline "Hello, \{fn\} \{ln\}".format(fn="abe", ln="jones") \newline Hello, abe jones \newline \newline "You owe me \$\{0:,.2f\}".format(253422.3) \newline You owe me \$253,422.30 \newline \newline now = datetime.now() \newline '\{:\%Y-\%m-\%d \%H:\%M:\%S\}'.format(now) \newline 2012-05-16 15:04:33} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Exceptions}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{try: \newline except ExceptionName as e: \newline except (ExceptionName, OtherException) as e: \newline else: \newline \# do something when no exception \newline finally: \newline \# do something anyway, exception or not} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Code Snippets}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Loop Over Sequence \newline for index, value in enumerate(seq): \newline print("\{\} : \{\}".format(index, value)) \newline \newline Loop Over Dictionary \newline for key in sorted(dict): \newline print(dict{[}key{]}) \newline \newline Read a File \newline with open("filename", "r") as f: \newline for line in f: \newline line = line.rstrip("\textbackslash{}n") \# Strip newline \newline print(line)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Python Decorator}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{def wrap(func): \newline def wrapper({\emph{args, {\bf{kwargs): \newline \# do something about func \newline func(}}args, }}kwargs) \newline \# do something about func \newline return wrapper \newline \newline \# Apply decorator \newline def to\_decorate(...): \newline \# body \newline to\_decorate = wrap(to\_decorate) \newline \newline \# More idiomatic \newline @wrap \newline def to\_decorate(...): \newline \#body \newline \newline \newline from functools import wraps \newline @wraps(func) \newline def wrapper(...) \# to keep the name and doc from the wrapped function \newline \newline \# Decorator with args: make a decorator factory \newline def \seqsplit{decorator\_factory(factory\_args):} \newline def decorator(func): \newline def wrapper({\emph{args, {\bf{kwargs): \newline \# do something about func \newline func(}}args, }}kwargs) \newline \# do something about func \newline return wrapper \newline return decorator \newline \newline \newline @decorator\_factory(1,2...) \newline def to\_decorate(...):} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Iterator}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{class zrange\_iter: \newline def \_\_init\_\_(self, n): \newline self.i = 0 \newline self.n = n \newline \newline def \_\_iter\_\_(self): \newline \# Iterators are iterables too. \newline \# Adding this functions to make them so. \newline return self \newline \newline def next(self): \newline if self.i \textless{} self.n: \newline i = self.i \newline self.i += 1 \newline return i \newline else: \newline raise StopIteration()} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Generator}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{def firstn(n): \newline num = 0 \newline while num \textless{} n: \newline yield num \newline num += 1 \newline \newline sum\_of\_first\_n = sum(firstn(1000000)) \newline \newline \# yield from my\_gen(x) \newline \newline g = my\_gen(x) \newline try: \newline next(g) \newline except StopIteration: \newline pass \newline \newline \# Unpacking Generators \newline \newline \textgreater{}\textgreater{}\textgreater{} g1 = (x for x in range(3)) \newline \textgreater{}\textgreater{}\textgreater{} g2 = (x{\emph{*2 for x in range(2)) \newline \textgreater{}\textgreater{}\textgreater{} {[}1, }}g1, 2, {\emph{g2{]} \newline {[}1, 0, 1, 2, 2, 0, 1{]} \newline \newline \textgreater{}\textgreater{}\textgreater{} g = (x for x in range(3)) \newline \textgreater{}\textgreater{}\textgreater{} a, b, c = g \newline \textgreater{}\textgreater{}\textgreater{} print(a, b, c) \newline 0 1 2 \newline \textgreater{}\textgreater{}\textgreater{} g = (x for x in range(6)) \newline \textgreater{}\textgreater{}\textgreater{} a, b, }}c, d = g \newline \textgreater{}\textgreater{}\textgreater{} print(a, b, d) \newline 0 1 5 \newline \textgreater{}\textgreater{}\textgreater{} print(c) \newline {[}2, 3, 4{]}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{IMPORTANTE: \newline \newline https://www.pythonsheets.com/notes/python-generator.html} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}