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

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

860

861

862

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

890

891

892

893

894

895

896

897

898

899

900

901

902

903

904

905

906

907

908

909

910

911

912

913

914

915

916

917

918

919

920

921

922

923

924

925

926

927

928

929

930

931

932

933

934

935

936

937

938

939

940

941

942

943

944

945

946

947

948

949

950

951

952

953

954

955

956

957

958

959

960

961

962

963

964

965

966

967

968

969

970

971

972

973

974

975

976

977

978

979

980

981

982

983

984

985

986

987

988

989

990

991

992

993

994

995

996

997

998

999

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1020

1021

1022

1023

1024

1025

1026

1027

1028

1029

1030

1031

1032

1033

1034

1035

1036

1037

1038

1039

1040

1041

1042

1043

1044

1045

1046

1047

1048

1049

1050

1051

1052

1053

1054

1055

1056

1057

1058

1059

1060

1061

1062

1063

1064

1065

1066

1067

1068

1069

1070

1071

1072

1073

1074

1075

1076

1077

1078

1079

1080

1081

1082

1083

1084

1085

1086

1087

1088

1089

1090

1091

1092

1093

1094

1095

1096

1097

1098

1099

1100

1101

1102

1103

1104

1105

1106

1107

1108

1109

1110

1111

1112

1113

1114

1115

1116

1117

1118

1119

1120

1121

1122

1123

1124

1125

1126

1127

1128

1129

1130

1131

1132

1133

1134

1135

1136

1137

1138

1139

1140

1141

1142

1143

1144

1145

1146

1147

1148

1149

1150

1151

1152

1153

1154

1155

1156

1157

1158

1159

1160

1161

1162

1163

1164

1165

1166

1167

1168

1169

1170

1171

1172

1173

1174

1175

1176

1177

1178

1179

1180

1181

1182

1183

1184

1185

1186

1187

1188

1189

1190

1191

1192

1193

1194

1195

1196

1197

1198

1199

1200

1201

1202

1203

1204

1205

1206

1207

1208

1209

1210

1211

1212

1213

1214

1215

1216

1217

1218

1219

1220

1221

1222

1223

1224

1225

1226

1227

1228

1229

1230

1231

1232

1233

1234

1235

1236

1237

1238

1239

1240

1241

1242

1243

1244

1245

1246

1247

1248

1249

1250

1251

1252

1253

1254

1255

1256

1257

1258

1259

1260

1261

1262

1263

1264

1265

1266

1267

1268

1269

1270

1271

1272

1273

1274

1275

1276

1277

1278

1279

1280

1281

1282

1283

1284

1285

1286

1287

1288

1289

1290

1291

1292

1293

1294

1295

1296

1297

1298

1299

1300

1301

1302

1303

1304

1305

1306

1307

1308

1309

1310

1311

1312

1313

1314

1315

1316

1317

1318

1319

1320

1321

1322

1323

1324

1325

1326

1327

1328

1329

1330

1331

1332

1333

1334

1335

1336

1337

1338

1339

1340

1341

1342

1343

1344

1345

1346

1347

1348

1349

1350

1351

1352

1353

1354

1355

1356

1357

1358

1359

1360

1361

1362

1363

1364

1365

1366

1367

1368

1369

1370

1371

1372

1373

1374

1375

1376

1377

1378

1379

1380

1381

1382

1383

1384

1385

1386

1387

1388

1389

#!/usr/bin/env python 

# -*- coding: UTF-8 -*- 

# 

# Copyright 2014 European Commission (JRC); 

# Licensed under the EUPL (the 'Licence'); 

# You may not use this work except in compliance with the Licence. 

# You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl 

""" 

The user-facing implementation of *xlref*. 

 

Prefer accessing the public members from the parent module. 

""" 

 

from abc import abstractmethod, ABCMeta 

from collections import namedtuple 

import json 

import logging 

import re 

from string import ascii_uppercase 

 

import six 

 

import itertools as itt 

import numpy as np 

import pandas as pd 

from six.moves.urllib.parse import urldefrag  # @UnresolvedImport 

 

log = logging.getLogger(__name__) 

 

try: 

    from xlrd import colname as xl_colname 

    # TODO: Try different backends providing `colname` function. 

except ImportError: 

    log.warning( 

        'One of `xlrd`, `...` libraries is needed, failures might occure later!') 

 

 

SKIP_CELLTYPE_CHECK = False 

"""When `True`, most coord-functions accept any 2-tuples.""" 

 

Cell = namedtuple('Cell', ['row', 'col']) 

""" 

A pair of 1-based strings, denoting the "A1" coordinates of a cell. 

 

The "num" coords (numeric, 0-based) are specified using numpy-arrays 

(:class:`Coords`). 

""" 

 

 

Coords = namedtuple('Coords', ['row', 'col']) 

""" 

A pair of 0-based integers denoting the "num" coordinates of a cell. 

 

The "A1" coords (1-based coordinates) are specified using :class:`Cell`. 

""" 

#     return np.array([row, cell], dtype=np.int16) 

 

 

def coords2Cell(row, col): 

    """Make *A1* :class:`Cell` from *resolved* coords, with rudimentary error-checking. 

 

    Examples:: 

 

        >>> coords2Cell(row=0, col=0) 

        Cell(row='1', col='A') 

        >>> coords2Cell(row=0, col=26) 

        Cell(row='1', col='AA') 

 

        >>> coords2Cell(row=10, col='.') 

        Cell(row='11', col='.') 

 

        >>> coords2Cell(row=-3, col=-2) 

        Traceback (most recent call last): 

        AssertionError: negative row! 

 

 

    """ 

    if row not in _special_coord_symbols: 

        assert row >= 0, 'negative row!' 

        row = str(row + 1) 

    if col not in _special_coord_symbols: 

        assert col >= 0, 'negative col!' 

        col = xl_colname(col) 

    return Cell(row=row, col=col) 

 

Edge = namedtuple('Edge', ['land', 'mov']) 

""" 

An :term:`Edge` might be "cooked" or "uncooked" depending on its `land`: 

 

- An *uncooked* edge contains *A1* :class:`Cell`. 

- An *cooked* edge contains a *resolved* :class:`Coords`. 

 

Use None for missing moves. 

""" 

 

 

def _uncooked_Edge(row, col, mov): 

    """ 

    Make a new `Edge` from any non-values supplied, as is capitalized, or nothing. 

 

    :param str, None col:    ie ``A`` 

    :param str, None row:    ie ``1`` 

    :param str, None mov:    ie ``RU1D?`` 

 

    :return:    a `Edge` if any non-None 

    :rtype:     Edge, None 

 

 

    Examples:: 

 

        >>> tr = _uncooked_Edge('1', 'a', 'Rul') 

        >>> tr 

        Edge(land=Cell(row='1', col='A'), mov='RUL') 

 

 

    No error checking performed:: 

 

        >>> _uncooked_Edge('Any', 'foo', 'BaR') 

        Edge(land=Cell(row='Any', col='FOO'), mov='BAR') 

 

        >>> print(_uncooked_Edge(None, None, None)) 

        None 

 

 

    except were coincidental:: 

 

        >>> _uncooked_Edge(row=0, col=123, mov='BAR') 

        Traceback (most recent call last): 

        AttributeError: 'int' object has no attribute 'upper' 

 

        >>> _uncooked_Edge(row=0, col='A', mov=123) 

        Traceback (most recent call last): 

        AttributeError: 'int' object has no attribute 'upper' 

    """ 

 

    if col == row == mov is None: 

        return None 

 

    return Edge(land=Cell(col=col and col.upper(), row=row), mov=mov and mov.upper()) 

 

_special_coord_symbols = {'^', '_', '.'} 

 

_primitive_dir_vectors = { 

    'L': Coords(0, -1), 

    'U': Coords(-1, 0), 

    'R': Coords(0, 1), 

    'D': Coords(1, 0) 

} 

 

_re_xl_ref_parser = re.compile( 

    r""" 

    ^\s*(?:(?P<sheet>[^!]+)?!)?                          # xl sheet name 

    (?:                                                  # first cell 

        (?P<st_col>[A-Z]+|_|\^)                          # first col 

        (?P<st_row>[123456789]\d*|_|\^)                  # first row 

        (?:\( 

            (?P<st_mov>L|U|R|D|LD|LU|UL|UR|RU|RD|DL|DR)  # moves from st cell 

            \) 

        )? 

    ) 

    (?::                                                 # second cell [opt] 

        (?P<nd_col>[A-Z]+|_|\^|\.)                       # second col 

        (?P<nd_row>[123456789]\d*|_|\^|\.)               # second row 

        (?:\( 

            (?P<nd_mov>L|U|R|D|LD|LU|UL|UR|RU|RD|DL|DR)  # moves from nd cell 

            \) 

        )? 

        (?:: 

            (?P<rect_exp>[LURD?123456789]+)              # rect expansion [opt] 

        )? 

    )? 

    \s* 

    (?::? 

        (?P<json>\{.*\})?                                # any json object [opt] 

    )\s*$""", 

    re.IGNORECASE | re.X) 

 

_re_rect_exp_splitter = re.compile('([LURD]\d+)', re.IGNORECASE) 

 

# TODO: Make rect_expansions `?` work different from numbers. 

_re_rect_expansion_parser = re.compile( 

    r""" 

    ^(?P<moves>[LURD]+)                                  # primitive moves 

    (?P<times>\?|\d+)?                                   # repetition times 

    $""", 

    re.IGNORECASE | re.X) 

 

 

def _repeat_moves(moves, times=None): 

    """ 

    Returns an iterator that repeats `moves` x `times`, or infinite if unspecified. 

 

    Used when parsing primitive :term:`directions`. 

 

   :param str moves: the moves to repeat 

   :param str times: N of repetitions. If `None` it means infinite repetitions. 

   :return:    An iterator of the moves 

   :rtype:     iterator 

 

    Examples:: 

 

         >>> list(_repeat_moves('LUR', '3')) 

         ['LUR', 'LUR', 'LUR'] 

         >>> list(_repeat_moves('ABC', '0')) 

         [] 

         >>> _repeat_moves('ABC')  ## infinite repetitions 

         repeat('ABC') 

     """ 

    args = (moves,) 

    if times is not None: 

        args += (int(times), ) 

    return itt.repeat(*args) 

 

 

def _parse_rect_expansions(rect_exp): 

    """ 

    Parse rect-expansion into a list of dir-letters iterables. 

 

    :param rect_exp: 

        A string with a sequence of primitive moves: 

        es. L1U1R1D1 

    :type xl_ref: str 

 

    :return: 

        A list of primitive-dir chains. 

    :rtype: list 

 

 

    Examples:: 

 

        >>> res = _parse_rect_expansions('LURD?') 

        >>> res 

        [repeat('LUR'), repeat('D', 1)] 

 

        # infinite generator 

        >>> [next(res[0]) for i in range(10)] 

        ['LUR', 'LUR', 'LUR', 'LUR', 'LUR', 'LUR', 'LUR', 'LUR', 'LUR', 'LUR'] 

 

        >>> list(res[1]) 

        ['D'] 

 

        >>> _parse_rect_expansions('1LURD') 

        Traceback (most recent call last): 

        ValueError: Invalid rect-expansion(1LURD) due to: 

                'NoneType' object has no attribute 'groupdict' 

 

    """ 

    try: 

        res = _re_rect_exp_splitter.split(rect_exp.replace('?', '1')) 

 

        return [_repeat_moves(**_re_rect_expansion_parser.match(v).groupdict()) 

                for v in res 

                if v != ''] 

 

    except Exception as ex: 

        msg = 'Invalid rect-expansion({}) due to: {}' 

        raise ValueError(msg.format(rect_exp, ex)) 

 

 

def parse_xl_ref(xl_ref): 

    """ 

    Parses a :term:`xl-ref` and splits it in its "ingredients". 

 

    :param str xl_ref: 

        a string with the following format: 

        <sheet>!<st_col><st_row>(<st_mov>):<nd_col><nd_row>(<nd_mov>): 

        <rect_exp>{<json>} 

        i.e.:: 

 

            sheet!A1(DR):Z20(UL):L1U2R1D1{"json":"..."} 

 

    :return: 

        dictionary containing the following parameters:: 

 

        - sheet: str 

        - st_edge: (Edge, None) the 1st-ref, uncooked, with raw cell 

        - nd_edge: (Edge, None) the 2nd-ref, uncooked, with raw cell 

        - rect_exp: (str) as found on the xl-ref 

        - json: parsed 

 

    :rtype: dict 

 

 

    Examples:: 

 

        >>> res = parse_xl_ref('Sheet1!A1(DR):Z20(UL):L1U2R1D1:{"json":"..."}') 

        >>> sorted(res.items()) 

        [('json', {'json': '...'}), 

         ('nd_edge', Edge(land=Cell(row='20', col='Z'), mov='UL')), 

         ('rect_exp', [repeat('L', 1), repeat('U', 2), repeat('R', 1), repeat('D', 1)]), 

         ('sheet', 'Sheet1'), 

         ('st_edge', Edge(land=Cell(row='1', col='A'), mov='DR'))] 

 

        >>> parse_xl_ref('A1(DR)Z20(UL)') 

        Traceback (most recent call last): 

        ValueError: Invalid xl-ref(A1(DR)Z20(UL)) due to: not an `xl-ref` syntax. 

    """ 

 

    try: 

        m = _re_xl_ref_parser.match(xl_ref) 

        if not m: 

            raise ValueError('not an `xl-ref` syntax.') 

        gs = m.groupdict() 

 

        # Replace coords of 1st and 2nd cells 

        #     with "uncooked" edge. 

        # 

        p = gs.pop 

        gs['st_edge'] = _uncooked_Edge(p('st_row'), p('st_col'), p('st_mov')) 

        gs['nd_edge'] = _uncooked_Edge(p('nd_row'), p('nd_col'), p('nd_mov')) 

 

        js = gs['json'] 

        gs['json'] = json.loads(js) if js else None 

 

        rect_exp = gs['rect_exp'] 

        gs['rect_exp'] = _parse_rect_expansions(rect_exp) if rect_exp else None 

 

        return gs 

 

    except Exception as ex: 

        msg = "Invalid xl-ref(%s) due to: %s" 

        log.debug(msg, xl_ref, ex, exc_info=1) 

        raise ValueError(msg % (xl_ref, ex)) 

 

 

def parse_xl_url(url, base_url=None, backend=None): 

    """ 

    Parses a :term:`xl-url`. 

 

    :param str url: 

        a string with the following format:: 

 

            <url_file>#<sheet>!<1st_edge>:<2nd_edge>:<expand><json> 

 

        Exxample:: 

 

            file:///path/to/file.xls#sheet_name!UP10:DN20:LDL1{"dim":2} 

    :param XlUrl base_url: 

    :param module backend: one of :mod:`_xlrd` or mod:`_xlwings` 

 

    :return: 

        dictionary containing the following parameters:: 

 

        - url_file 

        - sheet 

        - st_col 

        - st_row 

        - st_mov 

        - nd_col 

        - nd_row 

        - nd_mov 

        - json 

 

    :rtype: dict 

 

 

    Examples:: 

 

        >>> url = 'file:///sample.xlsx#Sheet1!A1(UL):.^(DR):LU?:{"2": "ciao"}' 

        >>> res = parse_xl_url(url) 

        >>> sorted(res.items()) 

        [('json', {'2': 'ciao'}), 

         ('nd_edge', Edge(land=Cell(row='^', col='.'), mov='DR')), 

         ('rect_exp', [repeat('L'), repeat('U', 1)]), 

         ('sheet', 'Sheet1'), 

         ('st_edge', Edge(land=Cell(row='1', col='A'), mov='UL')), 

         ('url_file', 'file:///sample.xlsx')] 

    """ 

 

    try: 

        url_file, frag = urldefrag(url) 

        res = parse_xl_ref(frag) 

        res['url_file'] = url_file 

 

        return res 

 

    except Exception as ex: 

        raise ValueError("Invalid xl-url({}) due to: {}".format(url, ex)) 

 

 

def _margin_coords_from_states_matrix(states_matrix): 

    """ 

    Returns top-left/bottom-down margins of full cells from a :term:`state` matrix. 

 

    May be used by :meth:`_Spreadsheet.get_margin_coords()` if a backend 

    does not report the sheet-margins internally. 

 

    :param np.ndarray states_matrix: 

            A 2D-array with `False` wherever cell are blank or empty. 

            Use :meth:`_Spreadsheet.get_states_matrix()` to derrive it. 

    :return:    the 2 coords of the top-left & bottom-right full cells 

    :rtype:     (Coords, Coords) 

 

    Examples:: 

 

        >>> states_matrix = np.asarray([ 

        ...    [0, 0, 0], 

        ...    [0, 1, 0], 

        ...    [0, 1, 1], 

        ...    [0, 0, 1], 

        ... ]) 

        >>> margins = _margin_coords_from_states_matrix(states_matrix) 

        >>> margins 

        (Coords(row=1, col=1), Coords(row=3, col=2)) 

 

 

    Note that the botom-left cell is not the same as `states_matrix` matrix size:: 

 

        >>> states_matrix = np.asarray([ 

        ...    [0, 0, 0, 0], 

        ...    [0, 1, 0, 0], 

        ...    [0, 1, 1, 0], 

        ...    [0, 0, 1, 0], 

        ...    [0, 0, 0, 0], 

        ... ]) 

        >>> _margin_coords_from_states_matrix(states_matrix) == margins 

        True 

 

    """ 

    if not states_matrix.any(): 

        c = Coords(0, 0) 

        return c, c 

    indices = np.array(np.where(states_matrix), dtype=np.int16).T 

 

    # return indices.min(0), indices.max(0) 

    return Coords(*indices.min(0)), Coords(*indices.max(0)) 

 

 

def _row2num(coord): 

    """ 

    Resolves special coords or converts Excel 1-based rows to zero-based, reporting invalids. 

 

    :param str, int coord:     excel-row coordinate or one of ``^_.`` 

    :return:    excel row number, >= 0 

    :rtype:     int 

 

    Examples:: 

 

        >>> row = _row2num('1') 

        >>> row 

        0 

        >>> row == _row2num(1) 

        True 

        >>> _row2num('-1') 

        -2 

 

    Fails ugly:: 

 

        >>> _row2num('.') 

        Traceback (most recent call last): 

        ValueError: invalid literal for int() with base 10: '.' 

    """ 

    return int(coord) - 1 

 

 

def _col2num(coord): 

    """ 

    Resolves special coords or converts Excel A1 columns to a zero-based, reporting invalids. 

 

    :param str coord:          excel-column coordinate or one of ``^_.`` 

    :return:    excel column number, >= 0 

    :rtype:     int 

 

    Examples:: 

 

        >>> col = _col2num('D') 

        >>> col 

        3 

        >>> _col2num('d') == col 

        True 

        >>> _col2num('AaZ') 

        727 

 

    Fails ugly:: 

 

        >>> _col2num('12') 

        Traceback (most recent call last): 

        ValueError: substring not found 

 

        >>> _col2num(1) 

        Traceback (most recent call last): 

        TypeError: 'int' object is not iterable 

    """ 

 

    rcoord = 0 

    for c in coord: 

        rcoord = rcoord * 26 + ascii_uppercase.rindex(c.upper()) + 1 

 

    rcoord -= 1 

 

    return rcoord 

 

 

def _resolve_coord(cname, cfunc, coord, up_coord, dn_coord, base_coord=None): 

    """ 

    Translates special coords or converts Excel string 1-based rows/cols to zero-based, reporting invalids. 

 

    :param str        cname:  the coord-name, one of 'row', 'column' 

    :param function   cfunc:  the function to convert coord ``str --> int`` 

    :param int, str   coord:  the "A1" coord to translate 

    :param int up_coord: 

            the resolved *top* or *left* margin coordinate 

    :param int up_coord: 

            the resolved *bottom* or *right* margin coordinate 

    :param int, None base_coord:  the resolved basis for dependent coord, if any 

 

    :return: the resolved coord or `None` if it were not a special coord. 

 

 

    Row examples:: 

 

        >>> cname = 'row' 

 

        >>> r0 = _resolve_coord(cname, _row2num, '1', 1, 10) 

        >>> r0 

        0 

        >>> r0 == _resolve_coord(cname, _row2num, 1, 1, 10) 

        True 

        >>> _resolve_coord(cname, _row2num, '^', 1, 10) 

        1 

        >>> _resolve_coord(cname, _row2num, '_', 1, 10) 

        10 

        >>> _resolve_coord(cname, _row2num, '.', 1, 10, 13) 

        13 

 

 

    But notice when base-cell missing:: 

 

        >>> _resolve_coord(cname, _row2num, '.', 0, 10, base_coord=None) 

        Traceback (most recent call last): 

        ValueError: invalid row('.') due to: '.' 

 

    Other ROW error-checks:: 

 

        >>> _resolve_coord(cname, _row2num, '0', 0, 10) 

        Traceback (most recent call last): 

        ValueError: invalid row('0') due to: resolved to negative(-1)! 

 

        >>> _resolve_coord(cname, _row2num, 'a', 0, 10) 

        Traceback (most recent call last): 

        ValueError: invalid row('a') due to: invalid literal for int() with base 10: 'a' 

 

        >>> _resolve_coord(cname, _row2num, None, 0, 10) 

        Traceback (most recent call last): 

        ValueError: invalid row(None) due to: 

                int() argument must be a string, 

                a bytes-like object or a number, not 'NoneType' 

 

 

    Column examples:: 

 

        >>> cname = 'column' 

 

        >>> _resolve_coord(cname, _col2num, 'A', 1, 10) 

        0 

        >>> _resolve_coord(cname, _col2num, 'DADA', 1, 10) 

        71084 

        >>> _resolve_coord(cname, _col2num, '.', 1, 10, 13) 

        13 

 

    And COLUMN error-checks:: 

 

        >>> _resolve_coord(cname, _col2num, None, 0, 10) 

        Traceback (most recent call last): 

        ValueError: invalid column(None) due to: 'NoneType' object is not iterable 

 

        >>> _resolve_coord(cname, _col2num, '4', 0, 10) 

        Traceback (most recent call last): 

        ValueError: invalid column('4') due to: substring not found 

 

        >>> _resolve_coord(cname, _col2num, 4, 0, 10) 

        Traceback (most recent call last): 

        ValueError: invalid column(4) due to: 'int' object is not iterable 

 

 

    """ 

    try: 

        if coord in _special_coord_symbols: 

            special_dict = { 

                '^': up_coord, 

                '_': dn_coord 

            } 

            if base_coord is not None: 

                special_dict['.'] = base_coord 

            rcoord = special_dict[coord] 

        else: 

            rcoord = cfunc(coord) 

 

        if rcoord < 0: 

            msg = 'resolved to negative(%s)!' 

            raise ValueError(msg % rcoord) 

 

        return rcoord 

    except Exception as ex: 

        msg = 'invalid {}({!r}) due to: {}' 

        six.raise_from(ValueError(msg.format(cname, coord, ex)), ex) 

 

 

def _resolve_cell(cell, up_coords, dn_coords, base_cords=None): 

    """ 

    Translates any special coords to absolute ones. 

 

    To get the margin_coords, use one of: 

 

    * :meth:`_Spreadsheet.get_margin_coords()` 

    * :func:`_margin_coords_from_states_matrix()` 

 

    :param Cell cell: 

            The "A1" cell to translate its coords. 

    :param Coords up_coords: 

            the top-left resolved coords with full-cells 

    :param Coords dn_coords: 

            the bottom-right resolved coords with full-cells 

    :param Coords base_cords: 

                A resolved cell to base dependent coords (``.``). 

    :return: the resolved cell-coords 

    :rtype:  Coords 

 

 

    Examples:: 

 

        >>> up = Coords(1, 2) 

        >>> dn = Coords(10, 6) 

        >>> base = Coords(40, 50) 

 

        >>> _resolve_cell(Cell(col='B', row=5), up, dn) 

        Coords(row=4, col=1) 

 

        >>> _resolve_cell(Cell('^', '^'), up, dn) 

        Coords(row=1, col=2) 

 

        >>> _resolve_cell(Cell('_', '_'), up, dn) 

        Coords(row=10, col=6) 

 

        >>> base == _resolve_cell(Cell('.', '.'), up, dn, base) 

        True 

 

        >>> _resolve_cell(Cell('1', '5'), up, dn) 

        Traceback (most recent call last): 

        ValueError: invalid cell(Cell(row='1', col='5')) due to: 

                invalid col('5') due to: substring not found 

 

        >>> _resolve_cell(Cell('A', 'B'), up, dn) 

        Traceback (most recent call last): 

        ValueError: invalid cell(Cell(row='A', col='B')) due to: 

                invalid row('A') due to: invalid literal for int() with base 10: 'A' 

 

    But notice when base-cell missing:: 

 

        >>> _resolve_cell(Cell('1', '.'), up, dn) 

        Traceback (most recent call last): 

        ValueError: invalid cell(Cell(row='1', col='.')) due to: invalid col('.') due to: '.' 

 

    """ 

    assert SKIP_CELLTYPE_CHECK or isinstance(cell, Cell), cell 

    assert SKIP_CELLTYPE_CHECK or isinstance(up_coords, Coords), up_coords 

    assert SKIP_CELLTYPE_CHECK or isinstance(dn_coords, Coords), dn_coords 

    try: 

        if base_cords is None: 

            base_row = base_col = None 

        else: 

            base_row, base_col = base_cords 

        row = _resolve_coord('row', _row2num, cell.row, 

                             up_coords[0], dn_coords[0], base_row) 

        col = _resolve_coord('col', _col2num, cell.col, 

                             up_coords[1], dn_coords[1], base_col) 

 

        return Coords(row, col) 

    except Exception as ex: 

        msg = "invalid cell(%s) due to: %s\n  margins(%s)\n  base_cords(%s)" 

        log.debug(msg, cell, ex, (up_coords, dn_coords), base_cords) 

        six.raise_from(ValueError("invalid cell(%s) due to: %s" % (cell, ex)), 

                       ex) 

 

 

_mov_slices = { 

    # VECTO_SLICE        REVERSE  COORD_INDEX 

    'L': (1, -1, lambda r, c: (r, slice(None, c + 1))), 

    'U': (0, -1, lambda r, c: (slice(None, r + 1), c)), 

    'R': (1, 1, lambda r, c: (r, slice(c, None))), 

    'D': (0, 1, lambda r, c: (slice(r, None), c)), 

} 

 

 

def _extract_states_vector(states_matrix, dn_coords, land, mov, 

                           mov_slices=_mov_slices): 

    coord_indx, is_reverse, slice_func = mov_slices[mov] 

    vect_slice = slice_func(*land) 

    states_vect = states_matrix[vect_slice] 

    if is_reverse < 0: 

        states_vect = states_vect[::-1] 

 

    return states_vect, coord_indx, is_reverse 

 

 

def _target_opposite(states_matrix, dn_coords, land, moves, 

                     primitive_dir_vectors=_primitive_dir_vectors): 

    """ 

    Scan row-by-row (or column-by-column) on specified `moves` and stop on the 1st full-cell. 

 

    :param np.ndarray states_matrix: 

            A 2D-array with `False` wherever cell are blank or empty. 

            Use :meth:`_Spreadsheet.get_states_matrix()` to derrive it. 

    :param Coords dn_coords: 

            the bottom-right for the top-left of full-cells 

    :param Coords land: 

            the landing-cell 

    :param moves: which MUST not be empty 

    :return: the identified target-cell's coordinates 

    :rtype: Coords 

 

 

    Examples:: 

 

        >>> states_matrix = np.array([ 

        ...     [0, 0, 0, 0, 0, 0], 

        ...     [0, 0, 0, 0, 0, 0], 

        ...     [0, 0, 0, 1, 1, 1], 

        ...     [0, 0, 1, 0, 0, 1], 

        ...     [0, 0, 1, 1, 1, 1] 

        ... ]) 

        >>> args = (states_matrix, Coords(4, 5)) 

 

        >>> _target_opposite(*(args + (Coords(0, 0), 'DR'))) 

        Coords(row=3, col=2) 

 

        >>> _target_opposite(*(args + (Coords(0, 0), 'RD'))) 

        Coords(row=2, col=3) 

 

    It fails if a non-empty target-cell cannot be found, or 

    it ends-up beyond bounds:: 

 

        >>> _target_opposite(*(args + (Coords(0, 0), 'D'))) 

        Traceback (most recent call last): 

        ValueError: No opposite-target for landing-Coords(row=0, col=0) with movement(D)! 

 

        >>> _target_opposite(*(args + (Coords(0, 0), 'UR'))) 

        Traceback (most recent call last): 

        ValueError: No opposite-target for landing-Coords(row=0, col=0) with movement(UR)! 

 

 

    But notice that the landing-cell maybe outside of bounds:: 

 

        >>> _target_opposite(*(args + (Coords(3, 10), 'L'))) 

        Coords(row=3, col=5) 

 

    """ 

    assert SKIP_CELLTYPE_CHECK or isinstance(dn_coords, Coords), dn_coords 

    assert SKIP_CELLTYPE_CHECK or isinstance(land, Coords), land 

 

    up_coords = np.array([0, 0]) 

    target = np.array(land) 

 

    if land[0] > dn_coords[0] and 'U' in moves: 

        target[0] = dn_coords[0] 

    if land[1] > dn_coords[1] and 'L' in moves: 

        target[1] = dn_coords[1] 

 

#     if states_matrix[target].all(): 

#         return Coords(*target) 

 

    imoves = iter(moves) 

    mov1 = next(imoves) 

    mov2 = next(imoves, None) 

    dv2 = mov2 and primitive_dir_vectors[mov2] 

 

    # Limit negative coords, since they are valid indices. 

    while (up_coords <= target).all(): 

        try: 

            states_vect, coord_indx, is_reverse = _extract_states_vector( 

                states_matrix, dn_coords, target, mov1) 

        except IndexError: 

            break 

        else: 

            if states_vect.any(): 

                indices = states_vect.nonzero()[0] 

                target[coord_indx] += is_reverse * indices.min() 

 

                return Coords(*target) 

 

            if not dv2: 

                break 

 

            target += dv2 

 

    msg = 'No opposite-target for landing-{} with movement({})!' 

    raise ValueError(msg.format(land, moves)) 

 

 

def _target_same_vector(states_matrix, dn_coords, land, mov): 

    """ 

    :param np.ndarray states_matrix: 

            A 2D-array with `False` wherever cell are blank or empty. 

            Use :meth:`_Spreadsheet.get_states_matrix()` to derrive it. 

    :param Coords dn_coords: 

            the bottom-right for the top-left of full-cells 

    :param Coords dn_coords: 

            the bottom-right for the top-left of full-cells 

    :param Coords land: 

            The landing-cell, which MUST be full! 

    """ 

    states_vect, coord_indx, is_reverse = _extract_states_vector( 

        states_matrix, dn_coords, land, mov) 

    if states_vect.all(): 

        same_len = len(states_vect) - 1 

    else: 

        indices = np.diff(states_vect).nonzero()[0] 

        same_len = indices.min() 

    target_coord = land[coord_indx] + is_reverse * same_len 

 

    return target_coord, coord_indx 

 

 

def _target_same(states_matrix, dn_coords, land, moves): 

    """ 

    Scan term:`exterior` row and column on specified `moves` and stop on the last full-cell. 

 

    :param Coords states_matrix: 

            A 2D-array with `False` wherever cell are blank or empty. 

            Use :meth:`_Spreadsheet.get_states_matrix()` to derrive it. 

    :param Coords dn_coords: 

            the bottom-right for the top-left of full-cells 

    :param Coords land: 

            the landing-cell which MUST be within bounds 

    :param moves: which MUST not be empty 

    :return: the identified target-cell's coordinates 

    :rtype: Coords 

 

 

    Examples:: 

 

        >>> states_matrix = np.array([ 

        ...     [0, 0, 0, 0, 0, 0], 

        ...     [0, 0, 0, 0, 0, 0], 

        ...     [0, 0, 0, 1, 1, 1], 

        ...     [0, 0, 1, 0, 0, 1], 

        ...     [0, 0, 1, 1, 1, 1] 

        ... ]) 

        >>> args = (states_matrix, Coords(4, 5)) 

 

        >>> _target_same(*(args + (Coords(4, 5), 'U'))) 

        Coords(row=2, col=5) 

 

        >>> _target_same(*(args + (Coords(4, 5), 'L'))) 

        Coords(row=4, col=2) 

 

        >>> _target_same(*(args + (Coords(4, 5), 'UL', ))) 

        Coords(row=2, col=2) 

 

 

    It fails if landing is empty or beyond bounds:: 

 

        >>> _target_same(*(args + (Coords(2, 2), 'DR'))) 

        Traceback (most recent call last): 

        ValueError: No same-target for landing-Coords(row=2, col=2) with movement(DR)! 

 

        >>> _target_same(*(args + (Coords(10, 3), 'U'))) 

        Traceback (most recent call last): 

        ValueError: No same-target for landing-Coords(row=10, col=3) with movement(U)! 

 

    """ 

    assert SKIP_CELLTYPE_CHECK or isinstance(dn_coords, Coords), dn_coords 

    assert SKIP_CELLTYPE_CHECK or isinstance(land, Coords), land 

 

    target = np.array(land) 

    if (target <= dn_coords).all() and states_matrix[land]: 

        for mov in moves: 

            coord, indx = _target_same_vector(states_matrix, dn_coords, 

                                              np.array(land), mov) 

            target[indx] = coord 

 

        return Coords(*target) 

 

    msg = 'No same-target for landing-{} with movement({})!' 

    raise ValueError(msg.format(land, moves)) 

 

 

def _expand_rect(states_matrix, state, xl_rect, exp_mov): 

    """ 

    Applies the :term:`expansion-moves` based on the `states_matrix`. 

 

    :param state: 

    :param xl_rect: 

    :param Coords states_matrix: 

            A 2D-array with `False` wherever cell are blank or empty. 

            Use :meth:`_Spreadsheet.get_states_matrix()` to derrive it. 

    :param exp_mov: 

    :return: 

 

 

    Examples:: 

 

        >>> states_matrix = np.array([ 

        ...     [0, 0, 0, 0, 0, 0, 0], 

        ...     [0, 0, 0, 0, 0, 0, 0], 

        ...     [0, 0, 0, 0, 0, 0, 0], 

        ...     [0, 0, 0, 0, 0, 0, 0], 

        ...     [0, 0, 0, 0, 0, 0, 0], 

        ...     [0, 0, 0, 0, 1, 1, 1], 

        ...     [0, 0, 0, 1, 0, 0, 1], 

        ...     [0, 0, 0, 1, 1, 1, 1] 

        ... ]) 

 

        >>> rng = (Coords(6, 3), Coords(6, 3)) 

        >>> exp_mov = [_repeat_moves('U')] 

        >>> _expand_rect(states_matrix, True, rng, exp_mov) 

        [Coords(row=6, col=3), Coords(row=6, col=3)] 

 

        >>> rng = (Coords(6, 3), Coords(7, 3)) 

        >>> exp_mov = [_repeat_moves('R')] 

        >>> _expand_rect(states_matrix, True, rng, exp_mov) 

        [Coords(row=6, col=3), Coords(row=7, col=6)] 

 

        >>> rng = (Coords(6, 3), Coords(10, 3)) 

        >>> exp_mov = [_repeat_moves('R')] 

        >>> _expand_rect(states_matrix, True, rng, exp_mov) 

        [Coords(row=6, col=3), Coords(row=10, col=6)] 

 

        >>> rng = (Coords(6, 5), Coords(6, 5)) 

        >>> exp_mov = [_repeat_moves('LURD')] 

        >>> _expand_rect(states_matrix, True, rng, exp_mov) 

        [Coords(row=5, col=3), Coords(row=7, col=6)] 

 

    """ 

    assert SKIP_CELLTYPE_CHECK or isinstance(xl_rect[0], Coords), xl_rect 

    assert SKIP_CELLTYPE_CHECK or isinstance(xl_rect[1], Coords), xl_rect 

    mov_indices = { 

        'L': (0, 1), 

        'U': (0, 1), 

        'R': (1, 0), 

        'D': (1, 0), 

    } 

    xl_rect = [np.array(v) for v in xl_rect] 

    for moves in exp_mov: 

        for directions in moves: 

            flag = True 

            for d in directions: 

                mv = _primitive_dir_vectors[d] 

                i, j = mov_indices[d] 

                st, nd = (xl_rect[i], xl_rect[j]) 

                st = st + mv 

                nd = [p2 if k == 0 else p1 for p1, p2, k in zip(st, nd, mv)] 

                if i == 1: 

                    v = states_matrix[nd[0]:st[0] + 1, nd[1]:st[1] + 1] 

                else: 

                    v = states_matrix[st[0]:nd[0] + 1, st[1]:nd[1] + 1] 

                if (not v.size and state) or (v != state).all(): 

                    continue 

                xl_rect[i] = st 

                flag = False 

 

            if flag: 

                break 

 

    # return xl_rect 

    return [Coords(*v) for v in xl_rect] 

 

 

def resolve_capture_rect(states_matrix, up_coords, dn_coords, st_edge, 

                         nd_edge=None, rect_exp=None): 

    """ 

    Performs :term:`targeting`, :term:`capturing` and :term:`expansions` based on the :term:`st_states-matrix`. 

 

    To get the margin_coords, use one of: 

 

    * :meth:`_Spreadsheet.get_margin_coords()` 

    * :func:`_margin_coords_from_states_matrix()` 

 

    Its results can be fed into :func:`read_capture_values()`. 

 

    :param Coords states_matrix: 

            A 2D-array with `False` wherever cell are blank or empty. 

            Use :meth:`_Spreadsheet.get_states_matrix()` to derrive it. 

    :param Coords up_coords: 

            the top-left coords with full-cells 

    :param Coords dn_coords: 

            the bottom-right coords with full-cells 

    :param Edge st_edge: "uncooked" as matched by regex 

    :param Edge nd_edge: "uncooked" as matched by regex 

    :param list or none rect_exp: 

            the result of :func:`_parse_rect_expansions()` 

 

    :return:    a ``(Coords, Coords)`` with the 1st and 2nd :term:`capture-cell` 

                ordered from top-left --> bottom-right. 

    :rtype: tuple 

 

    Examples:: 

 

        >>> states_matrix = np.array([ 

        ...     [0, 0, 0, 0, 0, 0], 

        ...     [0, 0, 0, 0, 0, 0], 

        ...     [0, 0, 0, 1, 1, 1], 

        ...     [0, 0, 1, 0, 0, 1], 

        ...     [0, 0, 1, 1, 1, 1] 

        ... ], dtype=bool) 

        >>> up, dn = _margin_coords_from_states_matrix(states_matrix) 

 

        >>> st_edge = Edge(Cell('1', 'A'), 'DR') 

        >>> nd_edge = Edge(Cell('.', '.'), 'DR') 

        >>> resolve_capture_rect(states_matrix, up, dn, st_edge, nd_edge) 

        (Coords(row=3, col=2), Coords(row=4, col=2)) 

 

    Walking backwards:: 

 

        >>> st_edge = Edge(Cell('_', '_'), None) 

        >>> nd_edge = Edge(Cell('.', '.'), 'UL') 

        >>> rect = resolve_capture_rect(states_matrix, up, dn, st_edge, nd_edge) 

        >>> rect 

        (Coords(row=2, col=2), Coords(row=4, col=5)) 

 

        >>> st_edge = Edge(Cell('^', '_'), None) 

        >>> nd_edge = Edge(Cell('_', '^'), None) 

        >>> rect == resolve_capture_rect(states_matrix, up, dn, st_edge, nd_edge) 

        True 

 

    """ 

    assert SKIP_CELLTYPE_CHECK or isinstance(up_coords, Coords), up_coords 

    assert SKIP_CELLTYPE_CHECK or isinstance(dn_coords, Coords), dn_coords 

 

    st = _resolve_cell(st_edge.land, up_coords, dn_coords) 

    try: 

        st_state = states_matrix[st] 

    except IndexError: 

        st_state = False 

 

    if st_edge.mov is not None: 

        if st_state: 

            st = _target_same(states_matrix, dn_coords, st, st_edge.mov) 

        else: 

            st = _target_opposite(states_matrix, dn_coords, st, st_edge.mov) 

 

    if nd_edge is None: 

        capt_rect = (st, st) 

    else: 

        nd = _resolve_cell(nd_edge.land, up_coords, dn_coords, st) 

 

        if nd_edge.mov is not None: 

            try: 

                nd_state = states_matrix[nd] 

            except IndexError: 

                nd_state = False 

 

            mov = nd_edge.mov 

            if nd_state: 

                nd = _target_same(states_matrix, dn_coords, nd, mov) 

            else: 

                nd = _target_opposite(states_matrix, dn_coords, nd, mov) 

 

        # Order rect-cells. 

        # 

        c = np.array([st, nd], dtype=np.int16) 

        #capt_rect = c.min(0), c.max(0) 

        capt_rect = (Coords(*c.min(0).tolist()), Coords(*c.max(0).tolist())) 

 

    if rect_exp: 

        capt_rect = _expand_rect(states_matrix, st_state, capt_rect, rect_exp) 

 

    return capt_rect 

 

 

def read_capture_rect(sheet, xl_rect): 

    """ 

    Extracts :term:`capture-rect` values from excel-sheet and apply :term:`filters`. 

 

    :param sheet: 

            anything supporting the :func:`read_rect(states_matrix, xl_rect)` 

            such as the the :class:`_Spreadsheet` which can hide away 

            the backend-module . 

    :param (Coords, Coords) xl_rect: 

            the the top-left/bottom/right edges of capture-rect, inclusive 

    :return: the rect values TODO: pre-processed 

 

    .. testsetup:: 

        >>> import os, tempfile, xlrd, pandas as pd 

 

        >>> df = pd.DataFrame([ 

        ... # Cols: 0       1    2 

        ...        [None, None, None], 

        ...        [5.1,  6.1,  7.1] 

        ... ]) 

        >>> tmp = ''.join([tempfile.mkstemp()[1], '.xlsx']) 

        >>> writer = pd.ExcelWriter(tmp) 

        >>> df.to_excel(writer, 'Sheet1', startrow=5, startcol=3) 

        >>> writer.save() 

 

    Examples:: 

        >>> import xlrd 

        >>> from pandalone import xlref 

 

        >>> xwb = xlrd.open_workbook(tmp).sheet_by_name('Sheet1') 

        >>> sheet = xlref.wrap_sheet(xwb) 

        >>> sheet.get_states_matrix() 

        array([[False, False, False, False, False, False, False], 

           [False, False, False, False, False, False, False], 

           [False, False, False, False, False, False, False], 

           [False, False, False, False, False, False, False], 

           [False, False, False, False, False, False, False], 

           [False, False, False, False,  True,  True,  True], 

           [False, False, False,  True, False, False, False], 

           [False, False, False,  True,  True,  True,  True]], dtype=bool) 

 

        # minimum matrix in the sheet 

        >>> read_capture_rect(sheet, (Coords(5, 3), Coords(7, 6))) 

        [[None,  0,    1,    2], 

         [0,    None, None, None], 

         [1,     5.1,  6.1,  7.1]] 

 

        # single-value 

        >>> read_capture_rect(sheet, (Coords(6, 3), Coords(6, 3))) 

        [0] 

 

        # column 

        >>> read_capture_rect(sheet, (Coords(0, 3), Coords(7, 3))) 

        [None, None, None, None, None, None, 0, 1] 

 

        # row 

        >>> read_capture_rect(sheet, (Coords(5, 0), Coords(5, 6))) 

        [None, None, None, None, 0, 1, 2] 

 

        # row beyond sheet-limits 

        >>> read_capture_rect(sheet, (Coords(5, 0), Coords(5, 10))) 

        [None, None, None, None, 0, 1, 2, None, None, None, None] 

 

    .. testcleanup:: 

        >>> os.remove(tmp) 

    """ 

 

    table = sheet.read_rect(*xl_rect) 

 

    st_target, nd_target = xl_rect 

    # column 

    if nd_target[1] == st_target[1]: 

        table = [v[0] for v in table] 

 

    # row 

    if nd_target[0] == st_target[0]: 

        table = table[0] 

 

    if isinstance(table, list): 

        return table 

    else: 

        return [table] 

 

 

def _get_value_dim(value): 

    try: 

        if isinstance(value, list): 

            return 1 + _get_value_dim(value[0]) 

    except IndexError: 

        return 1 

    return 0 

 

 

def _redim_value(value, n): 

    if n > 0: 

        return [_redim_value(value, n - 1)] 

    elif n < 0: 

        if len(value) > 1: 

            raise Exception 

        return _redim_value(value[0], n + 1) 

    return value 

 

 

def _redim_captured_values(value, dim_min, dim_max=None): 

    """ 

    Reshapes the output value of :func:`read_capture_rect()`. 

 

    :param value: matrix or vector or value 

    :type value: list of lists, list, value 

 

    :param dim_min: minimum dimension or 'auto' 

    :type dim_min: int, None 

 

    :param dim_max: maximum dimension 

    :type dim_max: int, None 

 

    :return: reshaped value 

    :rtype: list of lists, list, value 

 

 

    Examples:: 

 

        >>> _redim_captured_values([1, 2], 2) 

        [[1, 2]] 

 

        >>> _redim_captured_values([[1, 2]], 1) 

        [[1, 2]] 

 

        >>> _redim_captured_values([[1, 2]], 1, 1) 

        [1, 2] 

 

        >>> _redim_captured_values([], 2) 

        [[]] 

 

        >>> _redim_captured_values([[1, 2]], 0, 0) 

        Traceback (most recent call last): 

        ValueError: Cannot reduce Captured-values dimension(2) to (0, 0)! 

 

    """ 

    val_dim = _get_value_dim(value) 

    try: 

        if val_dim < dim_min: 

            return _redim_value(value, dim_min - val_dim) 

        elif dim_max is not None and val_dim > dim_max: 

            return _redim_value(value, dim_max - val_dim) 

        return value 

    except: 

        # TODO: Make redimming use np-arrays. 

        msg = 'Cannot reduce Captured-values dimension({}) to ({}, {})!' 

        raise ValueError(msg.format(val_dim, dim_min, dim_max)) 

 

 

def _type_df_with_numeric_conversion(df, args, kws): 

    df = pd.DataFrame(args, kws) 

    return df.convert_objects(convert_numeric=True) 

 

_default_filters = { 

    None: {'fun': lambda x: x},  # TODO: Actually _redim_captured_values(). 

    'df': {'fun': pd.DataFrame}, 

    'df_num': {'fun': _type_df_with_numeric_conversion}, 

    'nparray': {'fun': np.array}, 

    'dict': {'fun': dict}, 

    'sorted': {'fun': sorted} 

} 

 

 

def _process_captured_values(value, func=None, args=(), kws=None, filters=None, 

                             available_filters=_default_filters): 

    """ 

    Processes the output value of :func:`read_capture_rect()` function. 

 

    FIXME: Actually use _process_captured_values()! 

 

    :param value: matrix or vector or a scalar-value 

    :type value: list of lists, list, value 

 

    :param str, None type: 

            The 1st-filter to apply, if missing, applies the mapping found in 

            the ``None --> <filter`` entry of the `available_filters` dict. 

    :param dict, None kws:  keyword arguments for the filter function 

    :param sequence, None args: 

            arguments for the type-function 

    :param list filters: 

            A list of 3-tuples ``(filter_callable, *args, **kws)`` 

            to further process rect-values. 

    :param dict available_filters: 

            Entries of ``<fun_names> --> <callables>`` for pre-configured 

            filters available to post-process rect-values. 

            The callable for `None` key will be always called 

            to the original values to ensure correct dimensionality 

    :return: processed rect-values 

    :rtype: given type, or list of lists, list, value 

 

 

    Examples:: 

 

        >>> value = [[1, 2], [3, 4], [5, 6]] 

        >>> res = _process_captured_values(value, func='dict') 

        >>> sorted(res.items()) 

        [(1, 2), 

         (3, 4), 

         (5, 6)] 

 

        >>> value = [[1, 9], [8, 10], [5, 11]] 

        >>> _process_captured_values(value, 

        ...     filters=[{'func':'sorted', 'kws':{'reverse': True}}]) 

        [[8, 10], 

         [5, 11], 

         [1, 9]] 

    """ 

    if not kws: 

        kws = {} 

    val = available_filters[func]['fun'](value, *args, **kws) 

    if filters: 

        for v in filters: 

            val = _process_captured_values(val, **v) 

    return val 

 

 

class _Spreadsheet(object): 

    """ 

    An abstract  delegating to backends excel-worksheets wrapper that is utilized by this module. 

 

    Use :func:`pandalone.xlref.wrap_sheet()` to create it. 

 

    :param np.array _states_matrix: 

            The :term:`st_states-matrix` cached, so recreate object 

            to refresh it. 

    :param dict _margin_coords: 

            limits used by :func:`_resolve_cell`, cached, so recreate object 

            to refresh it. 

 

    Resource management is outside of the scope of this class, 

    and must happen in the backend workbook/sheet instance. 

 

    *xlrd* examples:: 

 

        >>> import xlrd                                       #  doctest: +SKIP 

        >>> with xlrd.open_workbook(self.tmp) as wb:          #  doctest: +SKIP 

        ...     sheet = xlref.xlrdSheet(wb.sheet_by_name('Sheet1')) 

        ...     ## Do whatever 

 

    *win32* examples:: 

 

        >>> with dsgdsdsfsd as wb:          #  doctest: +SKIP 

        ...     sheet = xlref.win32Sheet(wb.sheet['Sheet1']) 

        TODO 

    """ 

    __metaclass__ = ABCMeta 

 

    _states_matrix = None 

    _margin_coords = None 

 

    def __init__(self, sheet): 

        self._sheet = sheet 

 

    @abstractmethod 

    def _read_states_matrix(self): 

        pass 

 

    def get_states_matrix(self): 

        """ 

        Read and cache the :term:`st_states-matrix` of the wrapped sheet. 

 

        :return:   A 2D-array with `False` wherever cell are blank or empty. 

        :rtype:     ndarray 

        """ 

        if self._states_matrix is None: 

            self._states_matrix = self._read_states_matrix() 

        return self._states_matrix 

 

    @abstractmethod 

    def read_rect(self, up_coords, dn_coords): 

        """ 

        Fecth the actual values contained in the from the backend Excel-sheet. 

 

        :param up_coords: 

                the top-left edges of capture-rect, inclusive 

        :param dn_coords: 

                the bottom-right edges of capture-rect, inclusive 

        :return: a 2D-list with the values with at least 1 element, 

                or an empty-list 

        :rtype: list 

        """ 

        pass 

 

    def _read_margin_coords(self): 

        """ 

        Override if possible to read (any of the) limits directly from the sheet. 

 

        :return:    the 2 coords of the top-left & bottom-right full cells; 

                    anyone coords can be None. 

                    By default returns ``(None, None)``. 

        :rtype:     (Coords, Coords) 

 

        """ 

        return None, None  # pragma: no cover 

 

    def get_margin_coords(self): 

        """ 

        Extract (and cache) margins either internally or from :func:`_margin_coords_from_states_matrix()`. 

 

        :return:    the resolved top-left and bottom-right :class:`Coords` 

        :rtype:     tuple 

 

 

        Examples:: 

 

            >>> sheet = _Spreadsheet(sheet=None) 

            >>> sheet._states_matrix = np.asarray([       ## Mock states_matrix. 

            ...    [0, 0, 0, 0], 

            ...    [1, 1, 0, 0], 

            ...    [0, 1, 1, 0], 

            ...    [0, 0, 1, 0], 

            ... ]) 

            >>> sheet.get_margin_coords() 

            (Coords(row=1, col=0), Coords(row=3, col=2)) 

 

        """ 

        if not self._margin_coords: 

            up, dn = self._read_margin_coords() 

            if up is None or dn is None: 

                sm = self.get_states_matrix() 

                up1, dn1 = _margin_coords_from_states_matrix(sm) 

                up = up or up1 

                dn = dn or dn1 

            self._margin_coords = up, dn 

 

        return self._margin_coords