Skip to content

Commit 7059006

Browse files
committed
CLN: Cleanup io/parsers even more
Big one is removing get_terminal_size function, leading to a cascade of simplifications.
1 parent b7318fb commit 7059006

File tree

9 files changed

+21
-150
lines changed

9 files changed

+21
-150
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/common.py

+2-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Common IO api utilities"""
22

33
import codecs
4-
import csv
54
from http.client import HTTPException # noqa
65
import mmap
76
import os
@@ -31,9 +30,6 @@
3130
'-nan', ''}
3231

3332

34-
_urlopen = urlopen
35-
36-
3733
_VALID_URLS = set(uses_relative + uses_netloc + uses_params)
3834
_VALID_URLS.discard('')
3935

@@ -163,7 +159,7 @@ def get_filepath_or_buffer(filepath_or_buffer, encoding=None,
163159
----------
164160
filepath_or_buffer : a url, filepath (str, py.path.local or pathlib.Path),
165161
or buffer
166-
compression : str, optional
162+
compression : {'gzip', 'bz2', 'zip', 'xz', None}, optional
167163
encoding : the encoding to use to decode bytes, default is 'utf-8'
168164
mode : str, optional
169165
@@ -177,7 +173,7 @@ def get_filepath_or_buffer(filepath_or_buffer, encoding=None,
177173
filepath_or_buffer = _stringify_path(filepath_or_buffer)
178174

179175
if _is_url(filepath_or_buffer):
180-
req = _urlopen(filepath_or_buffer)
176+
req = urlopen(filepath_or_buffer)
181177
content_encoding = req.headers.get('Content-Encoding', None)
182178
if content_encoding == 'gzip':
183179
# Override compression based on Content-Encoding header
@@ -492,12 +488,3 @@ def readline(self):
492488

493489
def next(self):
494490
return next(self.reader).encode("utf-8")
495-
496-
497-
def UnicodeReader(f, dialect=csv.excel, encoding="utf-8", **kwds):
498-
# ignore encoding
499-
return csv.reader(f, dialect=dialect, **kwds)
500-
501-
502-
def UnicodeWriter(f, dialect=csv.excel, encoding="utf-8", **kwds):
503-
return csv.writer(f, dialect=dialect, **kwds)

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/csvs.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from pandas.core.dtypes.missing import notna
2121

2222
from pandas.io.common import (
23-
UnicodeWriter, _get_handle, _infer_compression, get_filepath_or_buffer)
23+
_get_handle, _infer_compression, get_filepath_or_buffer)
2424

2525

2626
class CSVFormatter(object):
@@ -161,12 +161,11 @@ def save(self):
161161
doublequote=self.doublequote,
162162
escapechar=self.escapechar,
163163
quotechar=self.quotechar)
164-
if self.encoding == 'ascii':
165-
self.writer = csvlib.writer(f, **writer_kwargs)
166-
else:
167-
writer_kwargs['encoding'] = self.encoding
168-
self.writer = UnicodeWriter(f, **writer_kwargs)
169164

165+
if self.encoding != "ascii":
166+
writer_kwargs["encoding"] = self.encoding
167+
168+
self.writer = csvlib.writer(f, **writer_kwargs)
170169
self._save()
171170

172171
finally:

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

+1-115
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,6 @@
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
"""
14-
from __future__ import print_function
15-
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()
324

335

346
def is_terminal():
@@ -46,89 +18,3 @@ def is_terminal():
4618
return False
4719
else: # IPython in a terminal
4820
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/io/parsers.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@
4141
from pandas.core.tools import datetimes as tools
4242

4343
from pandas.io.common import (
44-
_NA_VALUES, BaseIterator, UnicodeReader, UTF8Recoder, _get_handle,
45-
_infer_compression, _validate_header_arg, get_filepath_or_buffer,
46-
is_file_like)
44+
_NA_VALUES, BaseIterator, UTF8Recoder, _get_handle, _infer_compression,
45+
_validate_header_arg, get_filepath_or_buffer, is_file_like)
4746
from pandas.io.date_converters import generic_parser
4847

4948
# BOM character (byte order mark)
@@ -2360,17 +2359,17 @@ class MyDialect(csv.Dialect):
23602359
dia.delimiter = sniffed.delimiter
23612360
if self.encoding is not None:
23622361
self.buf.extend(list(
2363-
UnicodeReader(StringIO(line),
2364-
dialect=dia,
2365-
encoding=self.encoding)))
2362+
csv.reader(StringIO(line),
2363+
dialect=dia,
2364+
encoding=self.encoding)))
23662365
else:
23672366
self.buf.extend(list(csv.reader(StringIO(line),
23682367
dialect=dia)))
23692368

23702369
if self.encoding is not None:
2371-
reader = UnicodeReader(f, dialect=dia,
2372-
encoding=self.encoding,
2373-
strict=True)
2370+
reader = csv.reader(f, dialect=dia,
2371+
encoding=self.encoding,
2372+
strict=True)
23742373
else:
23752374
reader = csv.reader(f, dialect=dia,
23762375
strict=True)

pandas/tests/io/formats/test_format.py

+1-1
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

0 commit comments

Comments
 (0)