Skip to content

Commit

Permalink
TYP: Typing hints in pandas/io/formats/{css,csvs}.py (#30398)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaharNaveh authored and jreback committed Dec 27, 2019
1 parent 698f689 commit 980d0da
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 37 deletions.
30 changes: 19 additions & 11 deletions pandas/io/formats/css.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
"""Utilities for interpreting CSS from Stylers for formatting non-HTML outputs
"""
Utilities for interpreting CSS from Stylers for formatting non-HTML outputs.
"""

import re
import warnings


class CSSWarning(UserWarning):
"""This CSS syntax cannot currently be parsed"""
"""
This CSS syntax cannot currently be parsed.
"""

pass


def _side_expander(prop_fmt: str):
def expand(self, prop, value):
def expand(self, prop, value: str):
tokens = value.split()
try:
mapping = self.SIDE_SHORTHANDS[len(tokens)]
Expand All @@ -28,12 +31,13 @@ def expand(self, prop, value):


class CSSResolver:
"""A callable for parsing and resolving CSS to atomic properties
"""
A callable for parsing and resolving CSS to atomic properties.
"""

def __call__(self, declarations_str, inherited=None):
""" the given declarations to atomic properties
"""
The given declarations to atomic properties.
Parameters
----------
Expand All @@ -46,8 +50,8 @@ def __call__(self, declarations_str, inherited=None):
Returns
-------
props : dict
Atomic CSS 2.2 properties
dict
Atomic CSS 2.2 properties.
Examples
--------
Expand All @@ -69,7 +73,6 @@ def __call__(self, declarations_str, inherited=None):
('font-size', '24pt'),
('font-weight', 'bold')]
"""

props = dict(self.atomize(self.parse(declarations_str)))
if inherited is None:
inherited = {}
Expand Down Expand Up @@ -235,10 +238,15 @@ def atomize(self, declarations):
expand_margin = _side_expander("margin-{:s}")
expand_padding = _side_expander("padding-{:s}")

def parse(self, declarations_str):
"""Generates (prop, value) pairs from declarations
def parse(self, declarations_str: str):
"""
Generates (prop, value) pairs from declarations.
In a future version may generate parsed tokens from tinycss/tinycss2
Parameters
----------
declarations_str : str
"""
for decl in declarations_str.split(";"):
if not decl.strip():
Expand Down
52 changes: 26 additions & 26 deletions pandas/io/formats/csvs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
import csv as csvlib
from io import StringIO
import os
from typing import List
from typing import Hashable, List, Mapping, Optional, Sequence, Union
import warnings
from zipfile import ZipFile

import numpy as np

from pandas._libs import writers as libwriters
from pandas._typing import FilePathOrBuffer

from pandas.core.dtypes.generic import (
ABCDatetimeIndex,
Expand All @@ -33,27 +34,26 @@ class CSVFormatter:
def __init__(
self,
obj,
path_or_buf=None,
sep=",",
na_rep="",
float_format=None,
path_or_buf: Optional[FilePathOrBuffer[str]] = None,
sep: str = ",",
na_rep: str = "",
float_format: Optional[str] = None,
cols=None,
header=True,
index=True,
index_label=None,
mode="w",
encoding=None,
compression="infer",
quoting=None,
header: Union[bool, Sequence[Hashable]] = True,
index: bool = True,
index_label: Optional[Union[bool, Hashable, Sequence[Hashable]]] = None,
mode: str = "w",
encoding: Optional[str] = None,
compression: Union[str, Mapping[str, str], None] = "infer",
quoting: Optional[int] = None,
line_terminator="\n",
chunksize=None,
chunksize: Optional[int] = None,
quotechar='"',
date_format=None,
doublequote=True,
escapechar=None,
date_format: Optional[str] = None,
doublequote: bool = True,
escapechar: Optional[str] = None,
decimal=".",
):

self.obj = obj

if path_or_buf is None:
Expand Down Expand Up @@ -154,14 +154,17 @@ def __init__(
if not index:
self.nlevels = 0

def save(self):
def save(self) -> None:
"""
Create the writer & save
Create the writer & save.
"""
# GH21227 internal compression is not used when file-like passed.
if self.compression and hasattr(self.path_or_buf, "write"):
msg = "compression has no effect when passing file-like object as input."
warnings.warn(msg, RuntimeWarning, stacklevel=2)
warnings.warn(
"compression has no effect when passing file-like object as input.",
RuntimeWarning,
stacklevel=2,
)

# when zip compression is called.
is_zip = isinstance(self.path_or_buf, ZipFile) or (
Expand Down Expand Up @@ -223,7 +226,6 @@ def save(self):
_fh.close()

def _save_header(self):

writer = self.writer
obj = self.obj
index_label = self.index_label
Expand Down Expand Up @@ -303,8 +305,7 @@ def _save_header(self):
encoded_labels.extend([""] * len(columns))
writer.writerow(encoded_labels)

def _save(self):

def _save(self) -> None:
self._save_header()

nrows = len(self.data_index)
Expand All @@ -321,8 +322,7 @@ def _save(self):

self._save_chunk(start_i, end_i)

def _save_chunk(self, start_i: int, end_i: int):

def _save_chunk(self, start_i: int, end_i: int) -> None:
data_index = self.data_index

# create the data for a chunk
Expand Down

0 comments on commit 980d0da

Please sign in to comment.