|
@ -883,18 +883,19 @@ creature = Creature(Point(<span class="hljs-number">0</span>, <span class="hljs |
|
|
<li><strong>Return value of repr() should be unambiguous and of str() readable.</strong></li> |
|
|
<li><strong>Return value of repr() should be unambiguous and of str() readable.</strong></li> |
|
|
<li><strong>If only repr() is defined, it will also be used for str().</strong></li> |
|
|
<li><strong>If only repr() is defined, it will also be used for str().</strong></li> |
|
|
</ul> |
|
|
</ul> |
|
|
<h4 id="strisusedby">Str() is used by:</h4> |
|
|
|
|
|
|
|
|
<h4 id="strusecases">Str() use cases:</h4> |
|
|
<pre><code class="python language-python hljs">print(<el>) |
|
|
<pre><code class="python language-python hljs">print(<el>) |
|
|
<span class="hljs-string">f'<span class="hljs-subst">{<el>}</span>'</span> |
|
|
|
|
|
|
|
|
print(<span class="hljs-string">f'<span class="hljs-subst">{<el>}</span>'</span>) |
|
|
<span class="hljs-keyword">raise</span> Exception(<el>) |
|
|
<span class="hljs-keyword">raise</span> Exception(<el>) |
|
|
logging.debug(<el>) |
|
|
logging.debug(<el>) |
|
|
csv.writer(<file>).writerow([<el>]) |
|
|
csv.writer(<file>).writerow([<el>]) |
|
|
</code></pre> |
|
|
</code></pre> |
|
|
<h4 id="reprisusedby">Repr() is used by:</h4> |
|
|
|
|
|
|
|
|
<h4 id="reprusecases">Repr() use cases:</h4> |
|
|
<pre><code class="python language-python hljs">print([<el>]) |
|
|
<pre><code class="python language-python hljs">print([<el>]) |
|
|
<span class="hljs-string">f'<span class="hljs-subst">{<el>!r}</span>'</span> |
|
|
|
|
|
|
|
|
print(<span class="hljs-string">f'<span class="hljs-subst">{<el>!r}</span>'</span>) |
|
|
<span class="hljs-meta">>>> </span><el> |
|
|
<span class="hljs-meta">>>> </span><el> |
|
|
loguru.logger.exception() |
|
|
loguru.logger.exception() |
|
|
|
|
|
Z = dataclasses.make_dataclass(<span class="hljs-string">'Z'</span>, [<span class="hljs-string">'a'</span>]); print(Z(<el>)) |
|
|
</code></pre> |
|
|
</code></pre> |
|
|
<h3 id="constructoroverloading">Constructor Overloading</h3> |
|
|
<h3 id="constructoroverloading">Constructor Overloading</h3> |
|
|
<pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <<span class="hljs-title">name</span>>:</span> |
|
|
<pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <<span class="hljs-title">name</span>>:</span> |
|
@ -1323,11 +1324,15 @@ value = args.<name> |
|
|
pickle.dump(an_object, file) |
|
|
pickle.dump(an_object, file) |
|
|
</code></pre> |
|
|
</code></pre> |
|
|
<h2 id="sqlite"><a href="#sqlite" name="sqlite">#</a>SQLite</h2> |
|
|
<h2 id="sqlite"><a href="#sqlite" name="sqlite">#</a>SQLite</h2> |
|
|
|
|
|
<p><strong>Server-less database engine that stores each database into separate file.</strong></p> |
|
|
<pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> sqlite3 |
|
|
<pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> sqlite3 |
|
|
db = sqlite3.connect(<span class="hljs-string">'<path>'</span>) <span class="hljs-comment"># Also ':memory:'.</span> |
|
|
db = sqlite3.connect(<span class="hljs-string">'<path>'</span>) <span class="hljs-comment"># Also ':memory:'.</span> |
|
|
... |
|
|
... |
|
|
db.close() |
|
|
db.close() |
|
|
</code></pre> |
|
|
</code></pre> |
|
|
|
|
|
<ul> |
|
|
|
|
|
<li><strong>New database will be created if path doesn't exist.</strong></li> |
|
|
|
|
|
</ul> |
|
|
<h3 id="read">Read</h3> |
|
|
<h3 id="read">Read</h3> |
|
|
<pre><code class="python language-python hljs">cursor = db.execute(<span class="hljs-string">'<query>'</span>) |
|
|
<pre><code class="python language-python hljs">cursor = db.execute(<span class="hljs-string">'<query>'</span>) |
|
|
<span class="hljs-keyword">if</span> cursor: |
|
|
<span class="hljs-keyword">if</span> cursor: |
|
@ -1335,19 +1340,27 @@ db.close() |
|
|
<list> = cursor.fetchall() <span class="hljs-comment"># Remaining rows.</span> |
|
|
<list> = cursor.fetchall() <span class="hljs-comment"># Remaining rows.</span> |
|
|
</code></pre> |
|
|
</code></pre> |
|
|
<ul> |
|
|
<ul> |
|
|
<li><strong>Returned values can be of type str, int, float or bytes.</strong></li> |
|
|
|
|
|
|
|
|
<li><strong>Returned values can be of type str, int, float, bytes or None.</strong></li> |
|
|
</ul> |
|
|
</ul> |
|
|
<h3 id="write">Write</h3> |
|
|
<h3 id="write">Write</h3> |
|
|
<pre><code class="python language-python hljs">db.execute(<span class="hljs-string">'<query>'</span>) |
|
|
<pre><code class="python language-python hljs">db.execute(<span class="hljs-string">'<query>'</span>) |
|
|
db.commit() |
|
|
db.commit() |
|
|
</code></pre> |
|
|
</code></pre> |
|
|
<h3 id="placeholders">Placeholders</h3> |
|
|
<h3 id="placeholders">Placeholders</h3> |
|
|
<pre><code class="python language-python hljs">db.execute(<span class="hljs-string">'<query>'</span>, <list/tuple>) <span class="hljs-comment"># Replaces '?' in query with value.</span> |
|
|
|
|
|
db.execute(<span class="hljs-string">'<query>'</span>, <dict/namedtuple>) <span class="hljs-comment"># Replaces ':<key>' with value.</span> |
|
|
|
|
|
|
|
|
<pre><code class="python language-python hljs">db.execute(<span class="hljs-string">'<query>'</span>, <list/tuple>) <span class="hljs-comment"># Replaces '?'s in query with values.</span> |
|
|
|
|
|
db.execute(<span class="hljs-string">'<query>'</span>, <dict/namedtuple>) <span class="hljs-comment"># Replaces ':<key>'s with values.</span> |
|
|
|
|
|
db.executemany(<span class="hljs-string">'<query>'</span>, <coll_of_above>) <span class="hljs-comment"># Runs execute() many times.</span> |
|
|
</code></pre> |
|
|
</code></pre> |
|
|
<ul> |
|
|
<ul> |
|
|
<li><strong>Passed values can be of type str, int, float, bytes, bool, datetime.date and datetime.datetme.</strong></li> |
|
|
|
|
|
|
|
|
<li><strong>Passed values can be of type str, int, float, bytes, None, bool, datetime.date or datetime.datetme.</strong></li> |
|
|
</ul> |
|
|
</ul> |
|
|
|
|
|
<h3 id="mysql">MySQL</h3> |
|
|
|
|
|
<pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install mysql-connector</span> |
|
|
|
|
|
<span class="hljs-keyword">from</span> mysql <span class="hljs-keyword">import</span> connector |
|
|
|
|
|
db = connector.connect(host=<str>, user=<str>, password=<str>, database=<str>) |
|
|
|
|
|
cursor = db.cursor() |
|
|
|
|
|
cursor.execute(<span class="hljs-string">'<query>'</span>) |
|
|
|
|
|
</code></pre> |
|
|
<h2 id="bytes"><a href="#bytes" name="bytes">#</a>Bytes</h2> |
|
|
<h2 id="bytes"><a href="#bytes" name="bytes">#</a>Bytes</h2> |
|
|
<p><strong>Bytes object is an immutable sequence of single bytes. Mutable version is called 'bytearray'.</strong></p> |
|
|
<p><strong>Bytes object is an immutable sequence of single bytes. Mutable version is called 'bytearray'.</strong></p> |
|
|
<pre><code class="python language-python hljs"><bytes> = <span class="hljs-string">b'<str>'</span> <span class="hljs-comment"># Only accepts ASCII characters and \x00 - \xff.</span> |
|
|
<pre><code class="python language-python hljs"><bytes> = <span class="hljs-string">b'<str>'</span> <span class="hljs-comment"># Only accepts ASCII characters and \x00 - \xff.</span> |
|
|