From da70489fc9a1d122b173ec0806aba3ae4b580f4f Mon Sep 17 00:00:00 2001 From: sradc Date: Wed, 23 Mar 2022 17:28:41 +0000 Subject: [PATCH] refactor: array_library.py, update docstrings, add license --- smallpebble/array_library.py | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/smallpebble/array_library.py b/smallpebble/array_library.py index 078cc6d..bba815d 100644 --- a/smallpebble/array_library.py +++ b/smallpebble/array_library.py @@ -1,7 +1,21 @@ -"""Allows SmallPebble to dynamically switch between NumPy/CuPy, -for its ndarray computations. The default is NumPy. +# Copyright 2021 Sidney Radcliffe +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +"""Allows SmallPebble to dynamically switch between using NumPy/CuPy, +as its ndarray library. The default is NumPy. -Note to SmallPebble devs: +To SmallPebble devs: Watch out for cases where NumPy and CuPy differ, e.g. np.add.at is cupy.scatter_add. """ @@ -32,7 +46,6 @@ def use(array_library: ModuleType) -> None: sp.use(numpy) ``` """ - global library library = array_library @@ -41,19 +54,17 @@ def __getattr__(name: str): """Make this module act as a proxy, for NumPy/CuPy. Here's an example: - ```python import smallpebble.array_library as array_library - + x = array_library.array([1, 2, 3]) ** 2 # <- a NumPy array ``` - In this example, a NumPy array is created, because `array_library.array` results in this function being called, which then calls `getattr(numpy, "array")`, which is NumPy's function for creating arrays. - (The above assumes that `library == numpy`, which is the + (The above assumes that `library == numpy`, which is the default. The same thing would happen but with CuPy arrays, - if `library == cupy`.) + if `library == cupy`.) """ return getattr(library, name)