Skip to content

Commit

Permalink
Clean up Python imports and add isort
Browse files Browse the repository at this point in the history
  • Loading branch information
stinodego authored and ritchie46 committed Jul 14, 2021
1 parent 38129bb commit 0174956
Show file tree
Hide file tree
Showing 22 changed files with 410 additions and 404 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ jobs:
pip install -r py-polars/build.requirements.txt
- name: Run formatting checks
run: |
black --check .
cd py-polars && black --check . && isort --check . && cd ..
- name: Run linting
run: |
cd py-polars && flake8 && cd ..
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ We use GitHub issues to track public bugs. Report a bug by [opening a new issue]
We test the code formatting in the CI pipelines. If you don't want these to fail, you need to format:

* **Rust** code with `$ cargo fmt`
* **Python** code with [black (version 20.8b1)](https://github.com/psf/black), running `$ black .`
* **Python** code with [black](https://github.com/psf/black) (version 21.6b0) and [isort](https://github.com/PyCQA/isort) (version 5.9.2). Run both from the `py-polars` directory with `$ black . && isort .`

## Linting
We use linters to enforce code quality. This will be checked in CI.
Expand Down
5 changes: 3 additions & 2 deletions py-polars/build.requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
maturin==0.11.0
pytest==5.3.1
black==20.8b1
black==21.6b0
isort~=5.9.2
mypy~=0.910
numpy
ghp-import
pandas
flake8
mypy~=0.910
3 changes: 2 additions & 1 deletion py-polars/legacy/pypolars/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys

from polars import *
from polars import __version__
import sys

print(
"WARNING!\npy-polars was renamed to polars, please install polars!\nhttps://pypi.org/project/polars/",
Expand Down
3 changes: 2 additions & 1 deletion py-polars/legacy/setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from setuptools import setup
from os import path

from setuptools import setup

this_directory = path.abspath(path.dirname(__file__))
with open(path.join(this_directory, "README.md"), encoding="utf-8") as f:
long_description = f.read()
Expand Down
6 changes: 3 additions & 3 deletions py-polars/polars/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# flake8: noqa
from .series import Series, wrap_s
from .frame import DataFrame, wrap_df, StringCache
from .datatypes import *
from .frame import DataFrame, StringCache, wrap_df
from .functions import *
from .lazy import *
from .datatypes import * # type: ignore
from .series import Series, wrap_s

# during docs building the binary code is not yet available
try:
Expand Down
28 changes: 9 additions & 19 deletions py-polars/polars/_html.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
"""
Module for formatting output data in HTML.
"""

import typing as tp
from textwrap import dedent
from types import TracebackType
from typing import (
Dict,
Iterable,
List,
Optional,
Type,
)
from typing import Dict, Iterable, Optional, Type

import polars as pl


class Tag:
def __init__(
self, elements: List[str], tag: str, attributes: Optional[Dict[str, str]] = None
self,
elements: tp.List[str],
tag: str,
attributes: Optional[Dict[str, str]] = None,
) -> None:
self.tag = tag
self.elements = elements
Expand Down Expand Up @@ -72,7 +70,7 @@ def div(inner: str) -> str:
class HTMLFormatter:
def __init__(self, df: "pl.DataFrame", max_cols: int = 75, max_rows: int = 40):
self.df = df
self.elements: List[str] = []
self.elements: tp.List[str] = []
self.max_cols = max_cols
self.max_rows = max_rows
self.row_idx: Iterable[int]
Expand Down Expand Up @@ -168,19 +166,11 @@ def write_style(self) -> None:
template = dedent("\n".join((template_first, template_mid, template_last)))
self.write(template)

def render(self) -> List[str]: # type: ignore
def render(self) -> tp.List[str]: # type: ignore
"""
Return the lines needed to render a HTML table.
"""
with Tag(self.elements, "div"):
self.write_style()
super().render()
return self.elements


if __name__ == "__main__":
df = pl.DataFrame({"a": [1, 2, 3], "b": [1, 2, 3]})

fmt = NotebookFormatter(df)

print("\n".join(fmt.render()))
7 changes: 4 additions & 3 deletions py-polars/polars/datatypes.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from _ctypes import _SimpleCData
import ctypes
import typing
import typing as tp
from typing import Any, Dict, Type

from _ctypes import _SimpleCData

__pdoc__ = {
"dtype_to_ctype": False,
"dtype_to_int": False,
Expand Down Expand Up @@ -132,7 +133,7 @@ class Categorical(DataType):


# Don't change the order of these!
dtypes: typing.List[Type[DataType]] = [
dtypes: tp.List[Type[DataType]] = [
Int8,
Int16,
Int32,
Expand Down
7 changes: 3 additions & 4 deletions py-polars/polars/ffi.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import numpy as np
import numpy.core
from numpy import ctypeslib
import ctypes
from typing import Any

import numpy as np

# https://stackoverflow.com/questions/4355524/getting-data-from-ctypes-array-into-numpy


Expand All @@ -26,7 +25,7 @@ def _ptr_to_numpy(ptr: int, len: int, ptr_type: Any) -> np.ndarray:
"""
ptr_ctype = ctypes.cast(ptr, ctypes.POINTER(ptr_type))
return ctypeslib.as_array(ptr_ctype, (len,))
return np.ctypeslib.as_array(ptr_ctype, (len,))


def _as_float_ndarray(ptr: int, size: int) -> np.ndarray:
Expand Down

0 comments on commit 0174956

Please sign in to comment.