Skip to content

Commit 4214842

Browse files
committed
Class, Json, SQLite, Image, Pandas
1 parent 6974f6a commit 4214842

File tree

2 files changed

+67
-67
lines changed

2 files changed

+67
-67
lines changed

README.md

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -975,15 +975,15 @@ class MyClass:
975975
def get_class_name(cls):
976976
return cls.__name__
977977
```
978-
* **Return value of str() should be readable and of repr() unambiguous.**
979-
* **If only repr() is defined, it will also be used for str().**
980-
* **Methods decorated with `'@staticmethod'` do not receive 'self' nor 'cls' as their first arg.**
981978

982979
```python
983980
>>> obj = MyClass(1)
984981
>>> obj.a, str(obj), repr(obj)
985982
(1, '1', 'MyClass(1)')
986983
```
984+
* **Return value of str() should be readable and of repr() unambiguous.**
985+
* **If only repr() is defined, it will also be used for str().**
986+
* **Methods decorated with `'@staticmethod'` do not receive 'self' nor 'cls' as their first argument.**
987987

988988
#### Expressions that call the str() method:
989989
```python
@@ -1769,22 +1769,22 @@ JSON
17691769

17701770
```python
17711771
import json
1772-
<str> = json.dumps(<object>) # Converts object to JSON string.
1773-
<object> = json.loads(<str>) # Converts JSON string to object.
1772+
<str> = json.dumps(<list/dict>) # Converts collection to JSON string.
1773+
<coll> = json.loads(<str>) # Converts JSON string to collection.
17741774
```
17751775

1776-
### Read Object from JSON File
1776+
### Read Collection from JSON File
17771777
```python
17781778
def read_json_file(filename):
17791779
with open(filename, encoding='utf-8') as file:
17801780
return json.load(file)
17811781
```
17821782

1783-
### Write Object to JSON File
1783+
### Write Collection to JSON File
17841784
```python
1785-
def write_to_json_file(filename, an_object):
1785+
def write_to_json_file(filename, list_or_dict):
17861786
with open(filename, 'w', encoding='utf-8') as file:
1787-
json.dump(an_object, file, ensure_ascii=False, indent=2)
1787+
json.dump(list_or_dict, file, ensure_ascii=False, indent=2)
17881788
```
17891789

17901790

@@ -1884,41 +1884,41 @@ def write_to_csv_file(filename, rows, mode='w', **csv_params):
18841884

18851885
SQLite
18861886
------
1887-
**A server-less database engine that stores each database into a separate file.**
1887+
**A server-less database engine that stores each database into its own file.**
18881888

18891889
```python
18901890
import sqlite3
1891-
<conn> = sqlite3.connect(<path>) # Opens existing or new file. Also ':memory:'.
1892-
<conn>.close() # Closes connection. Discards uncommitted data.
1891+
<conn> = sqlite3.connect(<path>) # Opens existing or new file. Also ':memory:'.
1892+
<conn>.close() # Closes connection. Discards uncommitted data.
18931893
```
18941894

18951895
### Read
18961896
```python
1897-
<cursor> = <conn>.execute('<query>') # Can raise a subclass of sqlite3.Error.
1898-
<tuple> = <cursor>.fetchone() # Returns next row. Also next(<cursor>).
1899-
<list> = <cursor>.fetchall() # Returns remaining rows. Also list(<cursor>).
1897+
<cursor> = <conn>.execute('<query>') # Can raise a subclass of sqlite3.Error.
1898+
<tuple> = <cursor>.fetchone() # Returns next row. Also next(<cursor>).
1899+
<list> = <cursor>.fetchall() # Returns remaining rows. Also list(<cursor>).
19001900
```
19011901

19021902
### Write
19031903
```python
1904-
<conn>.execute('<query>') # Can raise a subclass of sqlite3.Error.
1905-
<conn>.commit() # Saves all changes since the last commit.
1906-
<conn>.rollback() # Discards all changes since the last commit.
1904+
<conn>.execute('<query>') # Can raise a subclass of sqlite3.Error.
1905+
<conn>.commit() # Saves all changes since the last commit.
1906+
<conn>.rollback() # Discards all changes since the last commit.
19071907
```
19081908

19091909
#### Or:
19101910
```python
1911-
with <conn>: # Exits the block with commit() or rollback(),
1912-
<conn>.execute('<query>') # depending on whether any exception occurred.
1911+
with <conn>: # Exits the block with commit() or rollback(),
1912+
<conn>.execute('<query>') # depending on whether any exception occurred.
19131913
```
19141914

19151915
### Placeholders
19161916
```python
1917-
<conn>.execute('<query>', <list/tuple>) # Replaces '?'s in query with values.
1918-
<conn>.execute('<query>', <dict/namedtuple>) # Replaces ':<key>'s with values.
1919-
<conn>.executemany('<query>', <coll_of_above>) # Runs execute() multiple times.
1917+
<conn>.execute('<query>', <list/tuple>) # Replaces '?'s in query with values.
1918+
<conn>.execute('<query>', <dict/namedtuple>) # Replaces ':<key>'s with values.
1919+
<conn>.executemany('<query>', <coll_of_coll>) # Runs execute() multiple times.
19201920
```
1921-
* **Passed values can be of type str, int, float, bytes, None or bool (stored as 1 or 0).**
1921+
* **Passed values can be of type str, int, float, bytes, None, or bool (stored as 1 or 0).**
19221922

19231923
### Example
19241924
**Values are not actually saved in this example because `'conn.commit()'` is omitted!**
@@ -1936,10 +1936,10 @@ with <conn>: # Exits the block with commit()
19361936
```python
19371937
# $ pip3 install sqlalchemy
19381938
from sqlalchemy import create_engine, text
1939-
<engine> = create_engine('<url>') # Url: 'dialect://user:password@host/dbname'.
1940-
<conn> = <engine>.connect() # Creates a connection. Also <conn>.close().
1941-
<cursor> = <conn>.execute(text('<query>'), …) # Replaces ':<key>'s with keyword arguments.
1942-
with <conn>.begin(): ... # Exits the block with commit or rollback.
1939+
<engine> = create_engine('<url>') # Url: 'dialect://user:password@host/dbname'.
1940+
<conn> = <engine>.connect() # Creates a connection. Also <conn>.close().
1941+
<cursor> = <conn>.execute(text('<query>'), …) # Replaces ':<key>'s with keyword arguments.
1942+
with <conn>.begin(): ... # Exits the block with commit or rollback.
19431943
```
19441944

19451945
```text
@@ -2832,9 +2832,9 @@ from PIL import ImageDraw
28322832
<Draw>.point((x, y)) # Draws a point. Truncates floats into ints.
28332833
<Draw>.line((x1, y1, x2, y2 [, ...])) # To get anti-aliasing use Image's resize().
28342834
<Draw>.arc((x1, y1, x2, y2), deg1, deg2) # Draws in clockwise dir. Also pieslice().
2835-
<Draw>.rectangle((x1, y1, x2, y2)) # To rotate use Image's rotate() and paste().
2835+
<Draw>.rectangle((x1, y1, x2, y2)) # Also rounded_rectangle(), regular_polygon().
28362836
<Draw>.polygon((x1, y1, x2, y2, ...)) # Last point gets connected to the first.
2837-
<Draw>.ellipse((x1, y1, x2, y2)) # Also rounded_rectangle(), regular_polygon().
2837+
<Draw>.ellipse((x1, y1, x2, y2)) # To rotate use Image's rotate() and paste().
28382838
<Draw>.text((x, y), <str>, font=<Font>) # `<Font> = ImageFont.truetype(<path>, size)`
28392839
```
28402840
* **Use `'fill=<color>'` to set the primary color.**
@@ -3340,15 +3340,15 @@ c 6 7
33403340
+----------------+---------------+---------------+---------------+
33413341
```
33423342
* **All methods operate on columns by default. Pass `'axis=1'` to process the rows instead.**
3343-
* **Fifth result's columns are indexed with a multi-index. This means we need a tuple of column keys to specify a single column: `'<DF>.loc[row_k, (col_k_1, col_k_2)]'`.**
3343+
* **Fifth result's columns are indexed with a multi-index. This means we need a tuple of column keys to specify a column: `'<DF>.loc[row_key, (col_key_1, col_key_2)]'`.**
33443344

33453345
#### DataFrame — Multi-Index:
33463346
```python
33473347
<DF> = <DF>.xs(row_key, level=<int>) # Rows with key on passed level of multi-index.
33483348
<DF> = <DF>.xs(row_keys, level=<ints>) # Rows that have first key on first level, etc.
33493349
<DF> = <DF>.set_index(col_keys) # Combines multiple columns into a multi-index.
33503350
<S/DF> = <DF>.stack/unstack(level=-1) # Combines col keys with row keys or vice versa.
3351-
<DF> = <DF>.pivot_table(index=col_key/s, …) # `columns=col_key/s, values=col_key/s`.
3351+
<DF> = <DF>.pivot_table(index=col_key/s, …) # `columns=key/s, values=key/s, aggfunc='mean'`.
33523352
```
33533353

33543354
#### DataFrame — Encode, Decode:

0 commit comments

Comments
 (0)