Skip to content

Commit 46fc2fa

Browse files
committed
default value & typing hint dataclasses
1 parent dca8295 commit 46fc2fa

File tree

4 files changed

+146
-47
lines changed

4 files changed

+146
-47
lines changed

README.md

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ All contributions are welcome:
198198
- [setup.py](#setuppy)
199199
- [Dataclasses](#dataclasses)
200200
- [Features](#features)
201+
- [Default values](#default-values)
202+
- [Type hints](#type-hints)
201203
- [Virtual Environment](#virtual-environment)
202204
- [virtualenv](#virtualenv)
203205
- [pipenv](#pipenv)
@@ -539,26 +541,26 @@ The *and* Operator’s *Truth* Table:
539541

540542
| Expression | Evaluates to |
541543
| ----------------- | ------------ |
542-
| `True and True` | `True` |
543-
| `True and False` | `False` |
544-
| `False and True` | `False` |
545-
| `False and False` | `False` |
544+
| `True and True` | `True` |
545+
| `True and False` | `False` |
546+
| `False and True` | `False` |
547+
| `False and False` | `False` |
546548

547549
The *or* Operator’s *Truth* Table:
548550

549-
| Expression | Evaluates to |
550-
| ---------------- | -------------- |
551-
| `True or True` | `True` |
552-
| `True or False` | `True` |
553-
| `False or True` | `True` |
554-
| `False or False` | `False` |
551+
| Expression | Evaluates to |
552+
| ---------------- | ------------ |
553+
| `True or True` | `True` |
554+
| `True or False` | `True` |
555+
| `False or True` | `True` |
556+
| `False or False` | `False` |
555557

556558
The *not* Operator’s *Truth* Table:
557559

558-
| Expression | Evaluates to |
559-
| ------------ | ------------- |
560-
| `not True` | `False` |
561-
| `not False` | `True` |
560+
| Expression | Evaluates to |
561+
| ----------- | ------------ |
562+
| `not True` | `False` |
563+
| `not False` | `True` |
562564

563565
[*Return to the Top*](#python-cheatsheet)
564566

@@ -2257,13 +2259,13 @@ A List comprehension can be generated from a dictionary:
22572259

22582260
### Escape Characters
22592261

2260-
| Escape character | Prints as |
2261-
| ------------------ | -------------------- |
2262-
| `\'` | Single quote |
2263-
| `\"` | Double quote |
2264-
| `\t` | Tab |
2265-
| `\n` | Newline (line break) |
2266-
| `\\` | Backslash |
2262+
| Escape character | Prints as |
2263+
| ---------------- | -------------------- |
2264+
| `\'` | Single quote |
2265+
| `\"` | Double quote |
2266+
| `\t` | Tab |
2267+
| `\n` | Newline (line break) |
2268+
| `\\` | Backslash |
22672269
22682270
Example:
22692271

@@ -3126,7 +3128,7 @@ The dot-star will match everything except a newline. By passing re.DOTALL as the
31263128
| `\d`, `\w`, and `\s` | a digit, word, or space character, ectively. |
31273129
| `\D`, `\W`, and `\S` | anything except a digit, word, or space acter, respectively. |
31283130
| `[abc]` | any character between the brackets (such as a, b, ). |
3129-
| `[^abc]` | any character that isn’t between the brackets. |
3131+
| `[^abc]` | any character that isn’t between the brackets. |
31303132

31313133
[*Return to the Top*](#python-cheatsheet)
31323134

@@ -4115,7 +4117,7 @@ Logging levels provide a way to categorize your log messages by importance. Ther
41154117
| ---------- | -------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
41164118
| `DEBUG` | `logging.debug()` | The lowest level. Used for small details. Usually you care about these messages only when diagnosing problems. |
41174119
| `INFO` | `logging.info()` | Used to record information on general events in your program or confirm that things are working at their point in the program. |
4118-
| `WARNING` | `logging.warning()` | Used to indicate a potential problem that doesn’t prevent the program from working but might do so in the future. |
4120+
| `WARNING` | `logging.warning()` | Used to indicate a potential problem that doesn’t prevent the program from working but might do so in the future. |
41194121
| `ERROR` | `logging.error()` | Used to record an error that caused the program to fail to do something. |
41204122
| `CRITICAL` | `logging.critical()` | The highest level. Used to indicate a fatal error that has caused or is about to cause the program to stop running entirely. |
41214123

@@ -4494,6 +4496,43 @@ with dataclass
44944496

44954497
[*Return to the Top*](#python-cheatsheet)
44964498

4499+
### Default values
4500+
4501+
It is easy to add default values to the fields of your data class.
4502+
4503+
```python
4504+
>>> @dataclass
4505+
... class Product:
4506+
... name: str
4507+
... count: int = 0
4508+
... price: float = 0.0
4509+
...
4510+
>>> obj = Product("Python")
4511+
>>> obj.name
4512+
Python
4513+
>>> obj.count
4514+
0
4515+
>>> obj.price
4516+
0.0
4517+
```
4518+
4519+
### Type hints
4520+
4521+
It is mandatory to define the data type in dataclass. However, If you don't want specify the datatype then, use ```typing.Any```.
4522+
4523+
```python
4524+
>>> from dataclasses import dataclass
4525+
>>> from typing import Any
4526+
4527+
>>> @dataclass
4528+
... class WithoutExplicitTypes:
4529+
... name: Any
4530+
... value: Any = 42
4531+
...
4532+
```
4533+
4534+
[*Return to the Top*](#python-cheatsheet)
4535+
44974536
## Virtual Environment
44984537

44994538
The use of a Virtual Environment is to test python code in encapsulated environments and to also avoid filling the base Python installation with libraries we might use for only one project.

blog_files/pysheet.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3988,6 +3988,7 @@ with dataclass
39883988
>>> obj.val
39893989
2
39903990
```
3991+
39913992
### Default values
39923993

39933994
It is easy to add default values to the fields of your data class.
@@ -4021,7 +4022,7 @@ It is mandatory to define the data type in dataclass. However, If you don't want
40214022
... name: Any
40224023
... value: Any = 42
40234024
...
4024-
```
4025+
```
40254026

40264027
## Virtual Environment
40274028

python_cheat_sheet.ipynb

Lines changed: 82 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@
204204
"- [setup.py](#setuppy)\n",
205205
"- [Dataclasses](#dataclasses)\n",
206206
" - [Features](#features)\n",
207+
" - [Default values](#default-values)\n",
208+
" - [Type hints](#type-hints)\n",
207209
"- [Virtual Environment](#virtual-environment)\n",
208210
" - [virtualenv](#virtualenv)\n",
209211
" - [pipenv](#pipenv)\n",
@@ -846,26 +848,26 @@
846848
"\n",
847849
"| Expression | Evaluates to |\n",
848850
"| ----------------- | ------------ |\n",
849-
"| `True and True` | `True` |\n",
850-
"| `True and False` | `False` |\n",
851-
"| `False and True` | `False` |\n",
852-
"| `False and False` | `False` |\n",
851+
"| `True and True` | `True` |\n",
852+
"| `True and False` | `False` |\n",
853+
"| `False and True` | `False` |\n",
854+
"| `False and False` | `False` |\n",
853855
"\n",
854856
"The *or* Operator’s *Truth* Table:\n",
855857
"\n",
856-
"| Expression | Evaluates to |\n",
857-
"| ---------------- | -------------- |\n",
858-
"| `True or True` | `True` |\n",
859-
"| `True or False` | `True` |\n",
860-
"| `False or True` | `True` |\n",
861-
"| `False or False` | `False` |\n",
858+
"| Expression | Evaluates to |\n",
859+
"| ---------------- | ------------ |\n",
860+
"| `True or True` | `True` |\n",
861+
"| `True or False` | `True` |\n",
862+
"| `False or True` | `True` |\n",
863+
"| `False or False` | `False` |\n",
862864
"\n",
863865
"The *not* Operator’s *Truth* Table:\n",
864866
"\n",
865-
"| Expression | Evaluates to |\n",
866-
"| ------------ | ------------- |\n",
867-
"| `not True` | `False` |\n",
868-
"| `not False` | `True` |\n",
867+
"| Expression | Evaluates to |\n",
868+
"| ----------- | ------------ |\n",
869+
"| `not True` | `False` |\n",
870+
"| `not False` | `True` |\n",
869871
"\n",
870872
"[*Return to the Top*](#python-cheatsheet)\n",
871873
"\n",
@@ -3869,13 +3871,13 @@
38693871
"\n",
38703872
"### Escape Characters\n",
38713873
"\n",
3872-
"| Escape character | Prints as |\n",
3873-
"| ------------------ | -------------------- |\n",
3874-
"| `\\'` | Single quote |\n",
3875-
"| `\\\"` | Double quote |\n",
3876-
"| `\\t` | Tab |\n",
3877-
"| `\\n` | Newline (line break) |\n",
3878-
"| `\\\\` | Backslash |\n",
3874+
"| Escape character | Prints as |\n",
3875+
"| ---------------- | -------------------- |\n",
3876+
"| `\\'` | Single quote |\n",
3877+
"| `\\\"` | Double quote |\n",
3878+
"| `\\t` | Tab |\n",
3879+
"| `\\n` | Newline (line break) |\n",
3880+
"| `\\\\` | Backslash |\n",
38793881
"\n",
38803882
"Example:"
38813883
]
@@ -5435,7 +5437,7 @@
54355437
"| `\\d`, `\\w`, and `\\s` | a digit, word, or space character, ectively. |\n",
54365438
"| `\\D`, `\\W`, and `\\S` | anything except a digit, word, or space acter, respectively. |\n",
54375439
"| `[abc]` | any character between the brackets (such as a, b, ). |\n",
5438-
"| `[^abc]` | any character that isn’t between the brackets. |\n",
5440+
"| `[^abc]` | any character that isn’t between the brackets. |\n",
54395441
"\n",
54405442
"[*Return to the Top*](#python-cheatsheet)\n",
54415443
"\n",
@@ -7057,7 +7059,7 @@
70577059
"| ---------- | -------------------- | ------------------------------------------------------------------------------------------------------------------------------ |\n",
70587060
"| `DEBUG` | `logging.debug()` | The lowest level. Used for small details. Usually you care about these messages only when diagnosing problems. |\n",
70597061
"| `INFO` | `logging.info()` | Used to record information on general events in your program or confirm that things are working at their point in the program. |\n",
7060-
"| `WARNING` | `logging.warning()` | Used to indicate a potential problem that doesn’t prevent the program from working but might do so in the future. |\n",
7062+
"| `WARNING` | `logging.warning()` | Used to indicate a potential problem that doesn’t prevent the program from working but might do so in the future. |\n",
70617063
"| `ERROR` | `logging.error()` | Used to record an error that caused the program to fail to do something. |\n",
70627064
"| `CRITICAL` | `logging.critical()` | The highest level. Used to indicate a fatal error that has caused or is about to cause the program to stop running entirely. |\n",
70637065
"\n",
@@ -7648,6 +7650,63 @@
76487650
"2"
76497651
]
76507652
},
7653+
{
7654+
"cell_type": "markdown",
7655+
"metadata": {},
7656+
"source": [
7657+
"[*Return to the Top*](#python-cheatsheet)\n",
7658+
"\n",
7659+
"### Default values\n",
7660+
"\n",
7661+
"It is easy to add default values to the fields of your data class."
7662+
]
7663+
},
7664+
{
7665+
"cell_type": "code",
7666+
"execution_count": null,
7667+
"metadata": {},
7668+
"outputs": [],
7669+
"source": [
7670+
">>> @dataclass\n",
7671+
"... class Product:\n",
7672+
"... name: str\n",
7673+
"... count: int = 0\n",
7674+
"... price: float = 0.0\n",
7675+
"...\n",
7676+
">>> obj = Product(\"Python\")\n",
7677+
">>> obj.name\n",
7678+
"Python\n",
7679+
">>> obj.count\n",
7680+
"0\n",
7681+
">>> obj.price\n",
7682+
"0.0"
7683+
]
7684+
},
7685+
{
7686+
"cell_type": "markdown",
7687+
"metadata": {},
7688+
"source": [
7689+
"### Type hints\n",
7690+
"\n",
7691+
"It is mandatory to define the data type in dataclass. However, If you don't want specify the datatype then, use ```typing.Any```."
7692+
]
7693+
},
7694+
{
7695+
"cell_type": "code",
7696+
"execution_count": null,
7697+
"metadata": {},
7698+
"outputs": [],
7699+
"source": [
7700+
">>> from dataclasses import dataclass\n",
7701+
">>> from typing import Any\n",
7702+
"\n",
7703+
">>> @dataclass\n",
7704+
"... class WithoutExplicitTypes:\n",
7705+
"... name: Any\n",
7706+
"... value: Any = 42\n",
7707+
"..."
7708+
]
7709+
},
76517710
{
76527711
"cell_type": "markdown",
76537712
"metadata": {},

python_cheat_sheet.pdf

3.49 KB
Binary file not shown.

0 commit comments

Comments
 (0)