Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

# -*- coding: utf-8 -*- 

r""" 

Factory for Character-Based Art 

""" 

#******************************************************************************* 

# Copyright (C) 2013 Jean-Baptiste Priez <jbp@kerios.fr>, 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# 

# This code is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 

# General Public License for more details. 

# 

# The full text of the GPL is available at: 

# 

# http://www.gnu.org/licenses/ 

#******************************************************************************* 

from six import iteritems, string_types, text_type, binary_type 

 

from sage.structure.sage_object import SageObject 

 

 

class CharacterArtFactory(SageObject): 

 

def __init__(self, 

art_type, string_type, magic_method_name, 

parenthesis, square_bracet, curly_brace): 

r""" 

Abstract base class for character art factory 

 

This class is the common implementation behind 

:func:`~sage.typeset.ascii_art.ascii_art` and 

:func:`~sage.typeset.unicode_art.unicode_art` . 

 

INPUT: 

 

- ``art_type`` -- type of the character art (i.e. a subclass of 

:class:`~sage.typeset.character_art.CharacterArt`) 

 

- ``string_type`` -- type of strings (the lines in the 

character art, e.g. ``str`` or ``unicode``). 

 

- ``magic_method_name`` -- name of the Sage magic method (e.g. 

``'_ascii_art_'`` or ``'_unicode_art_'``). 

 

- ``parenthesis`` -- left/right pair of two multi-line 

symbols. The parenthesis, a.k.a. round brackets (used for printing 

tuples). 

 

- ``square_bracket`` -- left/right pair of two multi-line 

symbols. The square_brackets (used for printing lists). 

 

- ``curly_brace`` -- left/right pair of two multi-line 

symbols. The curly braces (used for printing sets). 

 

EXAMPLES:: 

 

sage: from sage.typeset.ascii_art import _ascii_art_factory as factory 

sage: type(factory) 

<class 'sage.typeset.character_art_factory.CharacterArtFactory'> 

""" 

self.art_type = art_type 

assert isinstance(string_type('a'), string_types) 

self.string_type = string_type 

assert magic_method_name in ['_ascii_art_', '_unicode_art_'] 

self.magic_method_name = magic_method_name 

self.left_parenthesis, self.right_parenthesis = parenthesis 

self.left_square_bracket, self.right_square_bracket = square_bracet 

self.left_curly_brace, self.right_curly_brace = curly_brace 

 

def build(self, obj, baseline=None): 

r""" 

Construct a character art representation. 

 

INPUT: 

 

- ``obj`` -- anything; the object whose ascii art representation 

we want 

- ``baseline`` -- (optional) the baseline of the object 

 

OUTPUT: 

 

Character art object. 

 

EXAMPLES:: 

 

sage: ascii_art(integral(exp(x+x^2)/(x+1), x)) 

/ 

| 

| 2 

| x + x 

| e 

| ------- dx 

| x + 1 

| 

/ 

 

TESTS:: 

 

sage: n = var('n') 

sage: ascii_art(sum(binomial(2 * n, n + 1) * x^n, n, 0, oo)) 

/ __________ \ 

-\2*x + \/ -4*x + 1 - 1/ 

-------------------------- 

__________ 

2*x*\/ -4*x + 1 

sage: ascii_art(list(DyckWords(3))) 

[ /\ ] 

[ /\ /\ /\/\ / \ ] 

[ /\/\/\, /\/ \, / \/\, / \, / \ ] 

sage: ascii_art(1) 

1 

""" 

if isinstance(obj, self.art_type): 

if baseline is not None: 

from copy import copy 

obj = copy(obj) 

obj._baseline = baseline 

return obj 

if isinstance(obj, SageObject): 

return self.build_from_magic_method(obj, baseline) 

if baseline is None: 

baseline = 0 

if isinstance(obj, (tuple, list, dict, set)): 

if obj.__class__ is tuple: 

return self.build_tuple(obj, baseline) 

elif obj.__class__ is dict: 

return self.build_dict(obj, baseline) 

elif obj.__class__ is list: 

return self.build_list(obj, baseline) 

else: 

return self.build_set(obj, baseline) 

else: 

return self.build_from_string(obj, baseline) 

 

def build_empty(self): 

""" 

Return the empty character art object 

 

OUTPUT: 

 

Character art instance. 

 

EXAMPLES:: 

 

sage: from sage.typeset.ascii_art import _ascii_art_factory as factory 

sage: str(factory.build_empty()) 

'' 

""" 

return self.art_type.empty() 

 

def build_from_magic_method(self, obj, baseline=None): 

""" 

Return the character art object created by the object's magic method 

 

OUTPUT: 

 

Character art instance. 

 

EXAMPLES:: 

 

sage: from sage.typeset.ascii_art import _ascii_art_factory as factory 

sage: out = factory.build_from_magic_method(identity_matrix(2)); out 

[1 0] 

[0 1] 

sage: type(out) 

<class 'sage.typeset.ascii_art.AsciiArt'> 

""" 

magic_method = getattr(obj, self.magic_method_name) 

ret = magic_method() 

if baseline is not None: 

ret._baseline = baseline 

return ret 

 

def build_from_string(self, obj, baseline=0): 

r""" 

Return the character art object created from splitting 

the object's string representation. 

 

INPUT: 

 

- ``obj`` -- utf-8 encoded byte string or unicode 

- ``baseline`` -- (default: 0) the baseline of the object 

 

OUTPUT: 

 

Character art instance. 

 

EXAMPLES:: 

 

sage: from sage.typeset.ascii_art import _ascii_art_factory as factory 

sage: out = factory.build_from_string('a\nbb\nccc') 

sage: out + out + out 

a a a 

bb bb bb 

ccccccccc 

sage: type(out) 

<class 'sage.typeset.ascii_art.AsciiArt'> 

 

TESTS:: 

 

sage: from sage.typeset.ascii_art import _ascii_art_factory as factory 

sage: factory.build_from_string(u'a\nbb\nccc') # same with unicode 

a 

bb 

ccc 

 

:: 

 

sage: a = factory.build_from_string('a\nbb\nccc', baseline=2) 

sage: a + ascii_art('<-') 

a <- 

bb 

ccc 

""" 

if self.string_type is text_type and not isinstance(obj, text_type): 

obj = binary_type(obj).decode('utf-8') 

if self.string_type is binary_type and not isinstance(obj, binary_type): 

obj = text_type(obj).encode('utf-8') 

return self.art_type(obj.splitlines(), baseline=baseline) 

 

def build_container(self, content, left_border, right_border, baseline=0): 

r""" 

Return character art for a container. 

 

INPUT: 

 

- ``content`` -- 

:class:`~sage.typeset.character_art.CharacterArt`; the 

content of the container, usually comma-separated entries 

 

- ``left_border`` -- 

:class:`~sage.typeset.symbols.CompoundSymbol`; the left 

border of the container 

 

- ``right_border`` -- 

:class:`~sage.typeset.symbols.CompoundSymbol`; the right 

border of the container 

 

- ``baseline`` -- (default: 0) the baseline of the object 

 

TESTS:: 

 

sage: l = ascii_art(list(DyckWords(3))) # indirect doctest 

sage: l 

[ /\ ] 

[ /\ /\ /\/\ / \ ] 

[ /\/\/\, /\/ \, / \/\, / \, / \ ] 

sage: l.get_breakpoints() 

[9, 17, 25, 33] 

""" 

w = content.width() 

h = content.height() 

left_border = left_border.character_art(h) 

right_border = right_border.character_art(h) 

lines = [] 

pad = self.string_type(' ') 

for left, line, right in zip(left_border, content, right_border): 

lines.append(left + pad + line.ljust(w) + pad + right) 

shift = len(left_border) + len(pad) 

basepoints = [bp + shift for bp in content.get_breakpoints()] 

return self.art_type(lines, basepoints, baseline=baseline) 

 

def build_set(self, s, baseline=0): 

r""" 

Return an character art output of a set. 

 

TESTS:: 

 

sage: ascii_art(set(DyckWords(3))) # indirect doctest 

{ /\ } 

{ /\ /\/\ /\ / \ } 

{ / \/\, / \, /\/\/\, /\/ \, / \ } 

""" 

comma = self.art_type([self.string_type(', ')], baseline=0) 

repr_elems = self.concatenate(s, comma) 

return self.build_container( 

repr_elems, self.left_curly_brace, self.right_curly_brace, 

baseline) 

 

def build_dict(self, d, baseline=0): 

r""" 

Return an character art output of a dictionary. 

 

TESTS:: 

 

sage: d = ascii_art({i:dw for i,dw in enumerate(DyckWords(3))}) # indirect doctest 

sage: d 

{ /\ } 

{ /\ /\ /\/\ / \ } 

{ 0:/\/\/\, 1:/\/ \, 2:/ \/\, 3:/ \, 4:/ \ } 

sage: d.get_breakpoints() 

[11, 21, 31, 41] 

""" 

comma = self.art_type([self.string_type(', ')], 

baseline=0, 

breakpoints=[1]) 

colon = self.art_type([self.string_type(':')], baseline=0) 

def concat_no_breakpoint(k,v): 

k = self.build(k) 

v = self.build(v) 

elt = k + colon + v 

elt._breakpoints.remove(k._l) 

elt._breakpoints.remove(k._l + 1) 

return elt 

repr_elems = self.concatenate( 

(concat_no_breakpoint(k, v) for k, v in iteritems(d)), 

comma) 

return self.build_container( 

repr_elems, self.left_curly_brace, self.right_curly_brace, 

baseline) 

 

def build_list(self, l, baseline=0): 

r""" 

Return an character art output of a list. 

 

TESTS:: 

 

sage: l = ascii_art(list(DyckWords(3))) # indirect doctest 

sage: l 

[ /\ ] 

[ /\ /\ /\/\ / \ ] 

[ /\/\/\, /\/ \, / \/\, / \, / \ ] 

sage: l.get_breakpoints() 

[9, 17, 25, 33] 

 

The breakpoints of the object are used as breakpoints:: 

 

sage: l = ascii_art([DyckWords(2).list(), DyckWords(2).list()]) 

sage: l.get_breakpoints() 

[9, 17, 25] 

""" 

comma = self.art_type([self.string_type(', ')], 

baseline=0, 

breakpoints=[1]) 

repr_elems = self.concatenate(l, comma) 

return self.build_container( 

repr_elems, self.left_square_bracket, self.right_square_bracket, 

baseline) 

 

def build_tuple(self, t, baseline=0): 

r""" 

Return an character art output of a tuple. 

 

TESTS:: 

 

sage: ascii_art(tuple(DyckWords(3))) # indirect doctest 

( /\ ) 

( /\ /\ /\/\ / \ ) 

( /\/\/\, /\/ \, / \/\, / \, / \ ) 

""" 

comma = self.art_type([self.string_type(', ')], 

baseline=0, 

breakpoints=[1]) 

repr_elems = self.concatenate(t, comma) 

return self.build_container( 

repr_elems, self.left_parenthesis, self.right_parenthesis, 

baseline) 

 

def concatenate(self, iterable, separator, empty=None, baseline=0): 

""" 

Concatenate multiple character art instances 

 

The breakpoints are set as the breakpoints of the ``separator`` 

together with the breakpoints of the objects in ``iterable``. 

If there is ``None``, the end of the separator is used. 

 

INPUT: 

 

- ``iterable`` -- iterable of character art 

 

- ``separable`` -- character art; the separator in-between the 

iterable 

 

- ``empty`` -- an optional character art which is returned if 

``iterable`` is empty 

 

- ``baseline`` -- (default: 0) the baseline of the object 

 

EXAMPLES:: 

 

sage: i2 = identity_matrix(2) 

sage: ascii_art(i2, i2, i2, sep=ascii_art(1/x)) 

1 1 

[1 0]-[1 0]-[1 0] 

[0 1]x[0 1]x[0 1] 

""" 

if empty is None: 

empty = self.build_empty() 

result = empty 

breakpoints = [] 

separator = self.build(separator) 

bk = separator.get_breakpoints() 

if not bk: 

bk = [separator._l] 

for obj in iterable: 

if result is not empty: 

l = result._l 

result += separator 

breakpoints.extend([l+x for x in bk]) 

l = result._l 

obj = self.build(obj) 

result += self.build(obj) 

breakpoints.extend([l+x for x in obj.get_breakpoints()]) 

result._breakpoints = breakpoints 

result._baseline = baseline 

return result 

 

def parse_keywords(self, kwds): 

""" 

Parse the keyword input given by the dict ``kwds``. 

 

INPUT: 

 

- ``kwds`` -- a dict 

 

OUTPUT: 

 

A triple: 

 

- the separator 

- the baseline 

- the baseline of the separator 

 

.. WARNING:: 

 

The input is a dict, not a list of keyword arguments. 

 

.. NOTE:: 

 

This will remove ``sep``/``separator`` and ``baseline`` 

from ``kwds`` if they are specified. 

 

TESTS:: 

 

sage: from sage.typeset.ascii_art import _ascii_art_factory as factory 

sage: d = {'sep': '2', 'baseline': 5} 

sage: factory.parse_keywords(d) 

('2', 5, None) 

sage: d 

{} 

 

:: 

 

sage: d = {'foo': '2', 'baseline': 5} 

sage: factory.parse_keywords(d) 

(, 5, None) 

sage: d 

{'foo': '2'} 

 

sage: d = {'sep': '2', 'separator': '2'} 

sage: factory.parse_keywords(d) 

Traceback (most recent call last): 

... 

ValueError: cannot specify both 'sep' and 'separator' 

""" 

empty = self.build_empty() 

sep = kwds.pop("sep", empty) 

if sep == empty: 

sep = kwds.pop("separator", empty) 

elif "separator" in kwds: 

raise ValueError("cannot specify both 'sep' and 'separator'") 

return sep, kwds.pop("baseline", None), kwds.pop("sep_baseline", None)