Skip to content

Commit 5444dae

Browse files
df_wrapcols() and make_left_formatter() helpers.
1 parent b68230d commit 5444dae

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

.gitignore

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,21 @@ __pycache__
77
.tox/
88
.cache/
99
*.code-workspace
10+
examples/.ipynb_checkpoints/
11+
examples/scratch_pad/.ipynb_checkpoints/
12+
examples/issue461.py
13+
examples/macd.py
14+
examples/mpf_demo2.py
15+
examples/price-movement_plots.retcalcvals.ipynb
16+
examples/scratch_pad/comp.csv
17+
examples/scratch_pad/issue466.py
18+
examples/scratch_pad/issues/.ipynb_checkpoints/
19+
examples/scratch_pad/issues/issue430.orig.py
20+
examples/scratch_pad/issues/issue430.py
21+
examples/scratch_pad/issues/issue455/
22+
examples/scratch_pad/issues/issue487_pie_chart.ipynb
23+
examples/scratch_pad/so70145937.ipynb
24+
examples/scratch_pad/so70240809.ipynb
25+
examples/scratch_pad/so70418851.py
26+
examples/scratch_pad/so70418851_data.csv
27+
examples/scratch_pad/so70498007.ipynb

examples/scratch_pad/fmtr.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
####################################################################
2+
#
3+
# https://stackoverflow.com/questions/25777037/how-can-i-left-justify-text-in-a-pandas-dataframe-column-in-an-ipython-notebook
4+
#
5+
####################################################################
6+
7+
import mplfinance as mpf
8+
import pandas as pd
9+
import textwrap
10+
11+
vk = mpf.plotting._valid_plot_kwargs()
12+
13+
df = (pd.DataFrame(vk).T.head(18)).drop('Validator',axis=1)
14+
15+
df['Kwarg'] = df.index.values
16+
df['Default'] = ["'"+d+"'" if isinstance(d,str) else str(d) for d in df['Default']]
17+
18+
df = df[['Kwarg','Default','Description']]
19+
df = df.head(5).append(df.tail(7))
20+
21+
# df.sort_index(inplace=True)
22+
23+
df
24+
25+
print('===========================')
26+
27+
print(df)
28+
29+
print('===========================')
30+
31+
def make_left_formatter(maxwidth):
32+
wm3 = maxwidth-3
33+
w = maxwidth
34+
def left_formatter(value):
35+
if not isinstance(value,str):
36+
return f'{value:<}'
37+
elif value[0:maxwidth] == '-'*maxwidth:
38+
return f'{value:<{w}.{w}s}'
39+
#elif len(value) > maxwidth and value[0:maxwidth] != '-'*maxwidth:
40+
elif len(value) > maxwidth:
41+
return f'{value:<{wm3}.{wm3}s}...'
42+
else:
43+
return f'{value:<{w}.{w}s}'
44+
return left_formatter
45+
46+
WRAPLEN=55
47+
48+
def df_wrapcol(df,wrap_column=None,wrap_length=None):
49+
50+
if wrap_column is None: return df
51+
if wrap_length is None: return df
52+
53+
index = []
54+
columns = {}
55+
for col in df.columns:
56+
columns[col] = []
57+
nonwrapcols = [col for col in df.columns if col != wrap_column]
58+
59+
for ix in df.index:
60+
row = df.loc[ix,]
61+
62+
swrap = str(row[wrap_column])
63+
tw = textwrap.wrap(swrap,wrap_length) if not swrap.isspace() else [' ']
64+
65+
columns[wrap_column].append(tw[0])
66+
index.append(str(ix))
67+
for col in nonwrapcols:
68+
columns[col].append(row[col])
69+
70+
if len(tw) > 1:
71+
for r in range(1,len(tw)):
72+
columns[wrap_column].append(tw[r])
73+
index.append(str(ix)+'.'+str(r))
74+
for col in nonwrapcols:
75+
columns[col].append(' ')
76+
77+
return pd.DataFrame(columns,index=index)
78+
79+
80+
df = df_wrapcol(df,wrap_column='Description',wrap_length=WRAPLEN)
81+
print('===========================')
82+
print('dfnew1=',df)
83+
84+
85+
# print('===========================')
86+
# df.columns = [ ' '+col for col in df.columns ]
87+
88+
dividers = []
89+
for col in df.columns:
90+
dividers.append('-'*int(df[col].str.len().max()))
91+
dfd = pd.DataFrame(dividers).T
92+
dfd.columns = df.columns
93+
dfd.index = pd.Index(['---'])
94+
95+
print('===========================')
96+
97+
df = dfd.append(df)
98+
99+
fmts = {'Kwarg': make_left_formatter(df['Kwarg'].str.len().max()+1),
100+
'Description': make_left_formatter(WRAPLEN),
101+
'Default': make_left_formatter(8),
102+
}
103+
s = df.to_string(formatters=fmts,index=False,justify='left')
104+
105+
print('\n ',s.replace('\n','\n '))
106+
107+
print('===========================')
108+

0 commit comments

Comments
 (0)