@@ -883,18 +883,19 @@ <h2 id="class"><a href="#class" name="class">#</a>Class</h2>
883
883
< li > < strong > Return value of repr() should be unambiguous and of str() readable.</ strong > </ li >
884
884
< li > < strong > If only repr() is defined, it will also be used for str().</ strong > </ li >
885
885
</ ul >
886
- < h4 id ="strisusedby "> Str() is used by :</ h4 >
886
+ < h4 id ="strusecases "> Str() use cases :</ h4 >
887
887
< pre > < code class ="python language-python hljs "> print(<el>)
888
- < span class ="hljs-string "> f'< span class ="hljs-subst "> {<el>}</ span > '</ span >
888
+ print( < span class ="hljs-string "> f'< span class ="hljs-subst "> {<el>}</ span > '</ span > )
889
889
< span class ="hljs-keyword "> raise</ span > Exception(<el>)
890
890
logging.debug(<el>)
891
891
csv.writer(<file>).writerow([<el>])
892
892
</ code > </ pre >
893
- < h4 id ="reprisusedby "> Repr() is used by :</ h4 >
893
+ < h4 id ="reprusecases "> Repr() use cases :</ h4 >
894
894
< pre > < code class ="python language-python hljs "> print([<el>])
895
- < span class ="hljs-string "> f'< span class ="hljs-subst "> {<el>!r}</ span > '</ span >
895
+ print( < span class ="hljs-string "> f'< span class ="hljs-subst "> {<el>!r}</ span > '</ span > )
896
896
< span class ="hljs-meta "> >>> </ span > <el>
897
897
loguru.logger.exception()
898
+ Z = dataclasses.make_dataclass(< span class ="hljs-string "> 'Z'</ span > , [< span class ="hljs-string "> 'a'</ span > ]); print(Z(<el>))
898
899
</ code > </ pre >
899
900
< h3 id ="constructoroverloading "> Constructor Overloading</ h3 >
900
901
< 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,31 +1324,43 @@ <h3 id="writeobjecttofile">Write Object to File</h3>
1323
1324
pickle.dump(an_object, file)
1324
1325
</ code > </ pre >
1325
1326
< h2 id ="sqlite "> < a href ="#sqlite " name ="sqlite "> #</ a > SQLite</ h2 >
1327
+ < p > < strong > Server-less database engine that stores each database into separate file.</ strong > </ p >
1326
1328
< pre > < code class ="python language-python hljs "> < span class ="hljs-keyword "> import</ span > sqlite3
1327
1329
db = sqlite3.connect(< span class ="hljs-string "> '<path>'</ span > ) < span class ="hljs-comment "> # Also ':memory:'.</ span >
1328
1330
...
1329
1331
db.close()
1330
1332
</ code > </ pre >
1333
+ < ul >
1334
+ < li > < strong > New database will be created if path doesn't exist.</ strong > </ li >
1335
+ </ ul >
1331
1336
< h3 id ="read "> Read</ h3 >
1332
1337
< pre > < code class ="python language-python hljs "> cursor = db.execute(< span class ="hljs-string "> '<query>'</ span > )
1333
1338
< span class ="hljs-keyword "> if</ span > cursor:
1334
1339
<tuple> = cursor.fetchone() < span class ="hljs-comment "> # First row.</ span >
1335
1340
<list> = cursor.fetchall() < span class ="hljs-comment "> # Remaining rows.</ span >
1336
1341
</ code > </ pre >
1337
1342
< ul >
1338
- < li > < strong > Returned values can be of type str, int, float or bytes .</ strong > </ li >
1343
+ < li > < strong > Returned values can be of type str, int, float, bytes or None .</ strong > </ li >
1339
1344
</ ul >
1340
1345
< h3 id ="write "> Write</ h3 >
1341
1346
< pre > < code class ="python language-python hljs "> db.execute(< span class ="hljs-string "> '<query>'</ span > )
1342
1347
db.commit()
1343
1348
</ code > </ pre >
1344
1349
< h3 id ="placeholders "> Placeholders</ h3 >
1345
- < 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 >
1346
- db.execute(< span class ="hljs-string "> '<query>'</ span > , <dict/namedtuple>) < span class ="hljs-comment "> # Replaces ':<key>' with value.</ span >
1350
+ < 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 >
1351
+ db.execute(< span class ="hljs-string "> '<query>'</ span > , <dict/namedtuple>) < span class ="hljs-comment "> # Replaces ':<key>'s with values.</ span >
1352
+ db.executemany(< span class ="hljs-string "> '<query>'</ span > , <coll_of_above>) < span class ="hljs-comment "> # Runs execute() many times.</ span >
1347
1353
</ code > </ pre >
1348
1354
< ul >
1349
- < li > < strong > Passed values can be of type str, int, float, bytes, bool, datetime.date and datetime.datetme.</ strong > </ li >
1355
+ < li > < strong > Passed values can be of type str, int, float, bytes, None, bool, datetime.date or datetime.datetme.</ strong > </ li >
1350
1356
</ ul >
1357
+ < h3 id ="mysql "> MySQL</ h3 >
1358
+ < pre > < code class ="python language-python hljs "> < span class ="hljs-comment "> # $ pip3 install mysql-connector</ span >
1359
+ < span class ="hljs-keyword "> from</ span > mysql < span class ="hljs-keyword "> import</ span > connector
1360
+ db = connector.connect(host=<str>, user=<str>, password=<str>, database=<str>)
1361
+ cursor = db.cursor()
1362
+ cursor.execute(< span class ="hljs-string "> '<query>'</ span > )
1363
+ </ code > </ pre >
1351
1364
< h2 id ="bytes "> < a href ="#bytes " name ="bytes "> #</ a > Bytes</ h2 >
1352
1365
< p > < strong > Bytes object is an immutable sequence of single bytes. Mutable version is called 'bytearray'.</ strong > </ p >
1353
1366
< 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 >
0 commit comments