Skip to content

Commit 9642c71

Browse files
Merge pull request #30 from abcnishant007/abcnishant007-patch-6
ENH: uses pprint's output directly
2 parents 6d444ae + e8d4beb commit 9642c71

2 files changed

Lines changed: 9 additions & 37 deletions

File tree

README.md

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -38,45 +38,17 @@ np.random.rand() : 0.649617730484109
3838
```
3939

4040

41-
#### 2. Now, `smartprint` includes pretty print (through `pprint` module) for lists and dicts as shown below:
41+
#### 2. `smartprint` includes pretty print (through `pprint` module and colored prints through [`rich`](https://github.com/Textualize/rich) module) for lists and dicts as shown below:
4242
##### 2 (a) List example
4343
```python
44-
a = [1, 4, 5, 3, 4, "This", "is", \
45-
["fun; The inspect module is"], \
46-
"really powerful","What",["do", [9, 8, 6],"you", "think?", 5]]
47-
sprint (a)
48-
```
49-
##### Output:
50-
```
51-
List: a
52-
[1,
53-
4,
54-
5,
55-
3,
56-
4,
57-
'This',
58-
'is',
59-
['fun; The inspect module is'],
60-
'really powerful',
61-
'What',
62-
['do', [9, 8, 6], 'you', 'think?', 5]]
63-
```
64-
##### 2 (b) Dict example
65-
```python
66-
a = {1:2, 3:"Kumar",31:"Nishant", \
67-
2.5:{1:2, 3:"Kumar",31:"Nishant", 0:"NK"}, 0:"NK"}
68-
sprint (a)
44+
45+
d = {-1:"dictionaries", 0: "are", 100:"keys", "boolean":[True, False],
46+
6:"printed", 50:" with sorted", "Nishant":"Kumar", \
47+
"numbers":[1,100,-2000,12]}
48+
sprint (d)
6949
```
7050
##### Output:
71-
```
72-
Dict: a
73-
Key: Value
74-
{0: 'NK',
75-
1: 2,
76-
2.5: {0: 'NK', 1: 2, 3: 'Kumar', 31: 'Nishant'},
77-
3: 'Kumar',
78-
31: 'Nishant'}
79-
```
51+
<img src="https://user-images.githubusercontent.com/9101260/196955542-4b6a97b8-92fa-42af-ad13-d7afbba86112.png" alt="drawing" width="320"/>
8052

8153

8254
## In place print replacement

src/smartprint/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import inspect
22
from rich import print as rprint
3-
3+
import pprint
44

55
def smartprint(*argv, **kwargs):
66
"""
@@ -36,4 +36,4 @@ def helper_smartprint_list(l, extracted_code):
3636
def helper_smartprint_dict(d, extracted_code):
3737
print("Dict:", extracted_code)
3838
print("Key: Value")
39-
rprint(dict(sorted(d.items(), key=lambda item: item[0])))
39+
rprint(pprint.pformat(d))

0 commit comments

Comments
 (0)