Skip to content

Commit 13d0511

Browse files
committed
dataclass to README.md, jn and index
1 parent 63eb119 commit 13d0511

5 files changed

Lines changed: 108 additions & 3 deletions

File tree

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ All contributions are welcome:
195195
- [`__main__` Top-level script environment](#main-top-level-script-environment)
196196
- [Advantages](#advantages)
197197
- [setup.py](#setuppy)
198+
- [Dataclasses](#dataclasses)
199+
- [Features](#features)
198200
- [Virtual Environment](#virtual-environment)
199201
- [virtualenv](#virtualenv)
200202
- [pipenv](#pipenv)
@@ -4436,6 +4438,45 @@ Find more information visit [http://docs.python.org/install/index.html](http://d
44364438

44374439
[*Return to the Top*](#python-cheatsheet)
44384440

4441+
## Dataclasses
4442+
4443+
`Dataclasses` are python classes but are suited for storing data objects.
4444+
This module provides a decorator and functions for automatically adding generated special methods such as `__init__()` and `__repr__()` to user-defined classes.
4445+
4446+
### Features
4447+
4448+
1. They store data and represent a certain data type. Ex: A number. For people familiar with ORMs, a model instance is a data object. It represents a specific kind of entity. It holds attributes that define or represent the entity.
4449+
4450+
2. They can be compared to other objects of the same type. Ex: A number can be greater than, less than, or equal to another number.
4451+
4452+
Python 3.7 provides a decorator dataclass that is used to convert a class into a dataclass.
4453+
4454+
python 2.7
4455+
4456+
```python
4457+
>>> class Number:
4458+
... def __init__(self, val):
4459+
... self.val = val
4460+
...
4461+
>>> obj = Number(2)
4462+
>>> obj.val
4463+
2
4464+
```
4465+
4466+
with dataclass
4467+
4468+
```python
4469+
>>> @dataclass
4470+
... class Number:
4471+
... val: int
4472+
...
4473+
>>> obj = Number(2)
4474+
>>> obj.val
4475+
2
4476+
```
4477+
4478+
[*Return to the Top*](#python-cheatsheet)
4479+
44394480
## Virtual Environment
44404481

44414482
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/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@
2020
- [Context Manager](#context-manager)
2121
- <a href="#__main__-Top-level-script-environment">__main__ Top-level script environment</a>
2222
- [setup.py](#setup.py)
23+
- [Dataclasses](#dataclasses)
2324
- [Virtual Environment](#virtual-environment)

blog_files/pysheet.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3936,11 +3936,10 @@ Our initial setup.py will also include information about the license and will re
39363936

39373937
Find more information visit [http://docs.python.org/install/index.html](http://docs.python.org/install/index.html).
39383938

3939-
39403939
## Dataclasses
39413940

3942-
```Dataclasses``` are python classes but are suited for storing data objects.
3943-
This module provides a decorator and functions for automatically adding generated special methods such as ```__init__()``` and ```__repr__()``` to user-defined classes.
3941+
`Dataclasses` are python classes but are suited for storing data objects.
3942+
This module provides a decorator and functions for automatically adding generated special methods such as `__init__()` and `__repr__()` to user-defined classes.
39443943

39453944
### Features
39463945

@@ -3951,6 +3950,7 @@ This module provides a decorator and functions for automatically adding generate
39513950
Python 3.7 provides a decorator dataclass that is used to convert a class into a dataclass.
39523951

39533952
python 2.7
3953+
39543954
```python
39553955
>>> class Number:
39563956
... def __init__(self, val):
@@ -3960,6 +3960,7 @@ python 2.7
39603960
>>> obj.val
39613961
2
39623962
```
3963+
39633964
with dataclass
39643965

39653966
```python
@@ -3971,6 +3972,7 @@ with dataclass
39713972
>>> obj.val
39723973
2
39733974
```
3975+
39743976
## Virtual Environment
39753977

39763978
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.

python_cheat_sheet.ipynb

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@
201201
"- [`__main__` Top-level script environment](#main-top-level-script-environment)\n",
202202
" - [Advantages](#advantages)\n",
203203
"- [setup.py](#setuppy)\n",
204+
"- [Dataclasses](#dataclasses)\n",
205+
" - [Features](#features)\n",
204206
"- [Virtual Environment](#virtual-environment)\n",
205207
" - [virtualenv](#virtualenv)\n",
206208
" - [pipenv](#pipenv)\n",
@@ -7564,6 +7566,65 @@
75647566
"source": [
75657567
"Find more information visit [http://docs.python.org/install/index.html](http://docs.python.org/install/index.html).\n",
75667568
"\n",
7569+
"[*Return to the Top*](#python-cheatsheet)\n",
7570+
"\n",
7571+
"## Dataclasses\n",
7572+
"\n",
7573+
"`Dataclasses` are python classes but are suited for storing data objects.\n",
7574+
"This module provides a decorator and functions for automatically adding generated special methods such as `__init__()` and `__repr__()` to user-defined classes.\n",
7575+
"\n",
7576+
"### Features\n",
7577+
"\n",
7578+
"1. They store data and represent a certain data type. Ex: A number. For people familiar with ORMs, a model instance is a data object. It represents a specific kind of entity. It holds attributes that define or represent the entity.\n",
7579+
"\n",
7580+
"2. They can be compared to other objects of the same type. Ex: A number can be greater than, less than, or equal to another number.\n",
7581+
"\n",
7582+
"Python 3.7 provides a decorator dataclass that is used to convert a class into a dataclass.\n",
7583+
"\n",
7584+
"python 2.7"
7585+
]
7586+
},
7587+
{
7588+
"cell_type": "code",
7589+
"execution_count": null,
7590+
"metadata": {},
7591+
"outputs": [],
7592+
"source": [
7593+
">>> class Number:\n",
7594+
"... def __init__(self, val):\n",
7595+
"... self.val = val\n",
7596+
"...\n",
7597+
">>> obj = Number(2)\n",
7598+
">>> obj.val\n",
7599+
"2"
7600+
]
7601+
},
7602+
{
7603+
"cell_type": "markdown",
7604+
"metadata": {},
7605+
"source": [
7606+
"with dataclass"
7607+
]
7608+
},
7609+
{
7610+
"cell_type": "code",
7611+
"execution_count": null,
7612+
"metadata": {},
7613+
"outputs": [],
7614+
"source": [
7615+
">>> @dataclass\n",
7616+
"... class Number:\n",
7617+
"... val: int\n",
7618+
"...\n",
7619+
">>> obj = Number(2)\n",
7620+
">>> obj.val\n",
7621+
"2"
7622+
]
7623+
},
7624+
{
7625+
"cell_type": "markdown",
7626+
"metadata": {},
7627+
"source": [
75677628
"[*Return to the Top*](#python-cheatsheet)\n",
75687629
"\n",
75697630
"## Virtual Environment\n",

python_cheat_sheet.pdf

3.02 KB
Binary file not shown.

0 commit comments

Comments
 (0)