Skip to content

Commit ec6060c

Browse files
committed
CLN: Clean up the IO directory yet even more
1 parent 4b6d9b7 commit ec6060c

File tree

7 files changed

+8
-141
lines changed

7 files changed

+8
-141
lines changed

pandas/core/arrays/categorical.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# pylint: disable=E1101,W0232
22

3+
from shutil import get_terminal_size
34
import textwrap
45
from warnings import warn
56

@@ -38,7 +39,6 @@
3839
from pandas.core.sorting import nargsort
3940

4041
from pandas.io.formats import console
41-
from pandas.io.formats.terminal import get_terminal_size
4242

4343
from .base import ExtensionArray, _extension_array_shared_docs
4444

pandas/core/series.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from __future__ import division
55

66
from collections import OrderedDict
7+
from shutil import get_terminal_size
78
from textwrap import dedent
89
import warnings
910

@@ -49,7 +50,6 @@
4950
from pandas.core.tools.datetimes import to_datetime
5051

5152
import pandas.io.formats.format as fmt
52-
from pandas.io.formats.terminal import get_terminal_size
5353
import pandas.plotting._core as gfx
5454

5555
# pylint: disable=E1101,E1103

pandas/io/formats/console.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Internal module for console introspection
33
"""
44

5-
from pandas.io.formats.terminal import get_terminal_size
5+
from shutil import get_terminal_size
66

77

88
def get_console_size():

pandas/io/formats/format.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from __future__ import print_function
88

99
from functools import partial
10+
from shutil import get_terminal_size
1011

1112
import numpy as np
1213

@@ -34,7 +35,6 @@
3435

3536
from pandas.io.common import _expand_user, _stringify_path
3637
from pandas.io.formats.printing import adjoin, justify, pprint_thing
37-
from pandas.io.formats.terminal import get_terminal_size
3838

3939
# pylint: disable=W0141
4040

pandas/io/formats/terminal.py

+2-113
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,9 @@
11
"""
2-
get_terminal_size() -- return width and height of terminal as a tuple
3-
4-
code from:
5-
http://stackoverflow.com/questions/566746/how-to-get-console- window-width-in-
6-
python
7-
8-
written by
9-
Harco Kuppens (http://stackoverflow.com/users/825214/harco-kuppens)
10-
11-
It is mentioned in the stackoverflow response that this code works
12-
on linux, os x, windows and cygwin (windows).
2+
Terminal utilities.
133
"""
144
from __future__ import print_function
155

16-
import os
17-
import shutil
18-
import subprocess
19-
20-
__all__ = ['get_terminal_size', 'is_terminal']
21-
22-
23-
def get_terminal_size():
24-
"""
25-
Detect terminal size and return tuple = (width, height).
26-
27-
Only to be used when running in a terminal. Note that the IPython notebook,
28-
IPython zmq frontends, or IDLE do not run in a terminal,
29-
"""
30-
31-
return shutil.get_terminal_size()
6+
__all__ = ['is_terminal']
327

338

349
def is_terminal():
@@ -46,89 +21,3 @@ def is_terminal():
4621
return False
4722
else: # IPython in a terminal
4823
return True
49-
50-
51-
def _get_terminal_size_windows():
52-
53-
try:
54-
from ctypes import windll, create_string_buffer
55-
56-
# stdin handle is -10
57-
# stdout handle is -11
58-
# stderr handle is -12
59-
60-
h = windll.kernel32.GetStdHandle(-12)
61-
csbi = create_string_buffer(22)
62-
res = windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
63-
except (AttributeError, ValueError):
64-
return None
65-
if res:
66-
import struct
67-
(bufx, bufy, curx, cury, wattr, left, top, right, bottom, maxx,
68-
maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw)
69-
sizex = right - left + 1
70-
sizey = bottom - top + 1
71-
return sizex, sizey
72-
else:
73-
return None
74-
75-
76-
def _get_terminal_size_tput():
77-
# get terminal width
78-
# src: http://stackoverflow.com/questions/263890/how-do-i-find-the-width
79-
# -height-of-a-terminal-window
80-
81-
try:
82-
proc = subprocess.Popen(["tput", "cols"],
83-
stdin=subprocess.PIPE,
84-
stdout=subprocess.PIPE)
85-
output_cols = proc.communicate(input=None)
86-
proc = subprocess.Popen(["tput", "lines"],
87-
stdin=subprocess.PIPE,
88-
stdout=subprocess.PIPE)
89-
output_rows = proc.communicate(input=None)
90-
except OSError:
91-
return None
92-
93-
try:
94-
# Some terminals (e.g. spyder) may report a terminal size of '',
95-
# making the `int` fail.
96-
97-
cols = int(output_cols[0])
98-
rows = int(output_rows[0])
99-
return cols, rows
100-
except (ValueError, IndexError):
101-
return None
102-
103-
104-
def _get_terminal_size_linux():
105-
def ioctl_GWINSZ(fd):
106-
try:
107-
import fcntl
108-
import termios
109-
import struct
110-
cr = struct.unpack(
111-
'hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
112-
except (struct.error, IOError):
113-
return None
114-
return cr
115-
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
116-
if not cr:
117-
try:
118-
fd = os.open(os.ctermid(), os.O_RDONLY)
119-
cr = ioctl_GWINSZ(fd)
120-
os.close(fd)
121-
except OSError:
122-
pass
123-
if not cr or cr == (0, 0):
124-
try:
125-
from os import environ as env
126-
cr = (env['LINES'], env['COLUMNS'])
127-
except (ValueError, KeyError):
128-
return None
129-
return int(cr[1]), int(cr[0])
130-
131-
132-
if __name__ == "__main__":
133-
sizex, sizey = get_terminal_size()
134-
print('width = {w} height = {h}'.format(w=sizex, h=sizey))

pandas/tests/io/formats/test_console.py

-19
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
import subprocess # noqa: F401
2-
31
import pytest
42

53
from pandas._config import detect_console_encoding
64

7-
from pandas.io.formats.terminal import _get_terminal_size_tput
8-
95

106
class MockEncoding(object): # TODO(py27): replace with mock
117
"""
@@ -76,18 +72,3 @@ def test_detect_console_encoding_fallback_to_default(monkeypatch, std, locale):
7672
context.setattr('sys.stdout', MockEncoding(std))
7773
context.setattr('sys.getdefaultencoding', lambda: 'sysDefaultEncoding')
7874
assert detect_console_encoding() == 'sysDefaultEncoding'
79-
80-
81-
@pytest.mark.parametrize("size", ['', ['']])
82-
def test_terminal_unknown_dimensions(monkeypatch, size, mocker):
83-
84-
def communicate(*args, **kwargs):
85-
return size
86-
87-
monkeypatch.setattr('subprocess.Popen', mocker.Mock())
88-
monkeypatch.setattr('subprocess.Popen.return_value.returncode', None)
89-
monkeypatch.setattr(
90-
'subprocess.Popen.return_value.communicate', communicate)
91-
result = _get_terminal_size_tput()
92-
93-
assert result is None

pandas/tests/io/formats/test_format.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from operator import methodcaller
1212
import os
1313
import re
14+
from shutil import get_terminal_size
1415
import sys
1516
import textwrap
1617
import warnings
@@ -32,7 +33,6 @@
3233

3334
import pandas.io.formats.format as fmt
3435
import pandas.io.formats.printing as printing
35-
from pandas.io.formats.terminal import get_terminal_size
3636

3737
use_32bit_repr = is_platform_windows() or is_platform_32bit()
3838

@@ -308,8 +308,6 @@ def test_repr_truncates_terminal_size(self, monkeypatch):
308308
# see gh-21180
309309

310310
terminal_size = (118, 96)
311-
monkeypatch.setattr('pandas.io.formats.console.get_terminal_size',
312-
lambda: terminal_size)
313311
monkeypatch.setattr('pandas.io.formats.format.get_terminal_size',
314312
lambda: terminal_size)
315313

@@ -338,8 +336,7 @@ def test_repr_truncates_terminal_size_full(self, monkeypatch):
338336
# GH 22984 ensure entire window is filled
339337
terminal_size = (80, 24)
340338
df = pd.DataFrame(np.random.rand(1, 7))
341-
monkeypatch.setattr('pandas.io.formats.console.get_terminal_size',
342-
lambda: terminal_size)
339+
343340
monkeypatch.setattr('pandas.io.formats.format.get_terminal_size',
344341
lambda: terminal_size)
345342
assert "..." not in str(df)

0 commit comments

Comments
 (0)