Skip to content

Commit

Permalink
Merge pull request #30 from abcnishant007/abcnishant007-patch-6
Browse files Browse the repository at this point in the history
ENH: uses pprint's output directly
  • Loading branch information
abcnishant007 authored Oct 20, 2022
2 parents 6d444ae + e8d4beb commit 9642c71
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 37 deletions.
42 changes: 7 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,45 +38,17 @@ np.random.rand() : 0.649617730484109
```


#### 2. Now, `smartprint` includes pretty print (through `pprint` module) for lists and dicts as shown below:
#### 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:
##### 2 (a) List example
```python
a = [1, 4, 5, 3, 4, "This", "is", \
["fun; The inspect module is"], \
"really powerful","What",["do", [9, 8, 6],"you", "think?", 5]]
sprint (a)
```
##### Output:
```
List: a
[1,
4,
5,
3,
4,
'This',
'is',
['fun; The inspect module is'],
'really powerful',
'What',
['do', [9, 8, 6], 'you', 'think?', 5]]
```
##### 2 (b) Dict example
```python
a = {1:2, 3:"Kumar",31:"Nishant", \
2.5:{1:2, 3:"Kumar",31:"Nishant", 0:"NK"}, 0:"NK"}
sprint (a)

d = {-1:"dictionaries", 0: "are", 100:"keys", "boolean":[True, False],
6:"printed", 50:" with sorted", "Nishant":"Kumar", \
"numbers":[1,100,-2000,12]}
sprint (d)
```
##### Output:
```
Dict: a
Key: Value
{0: 'NK',
1: 2,
2.5: {0: 'NK', 1: 2, 3: 'Kumar', 31: 'Nishant'},
3: 'Kumar',
31: 'Nishant'}
```
<img src="https://user-images.githubusercontent.com/9101260/196955542-4b6a97b8-92fa-42af-ad13-d7afbba86112.png" alt="drawing" width="320"/>


## In place print replacement
Expand Down
4 changes: 2 additions & 2 deletions src/smartprint/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import inspect
from rich import print as rprint

import pprint

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

0 comments on commit 9642c71

Please sign in to comment.