This repository was archived by the owner on Mar 26, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathcode.py
More file actions
65 lines (49 loc) · 2.31 KB
/
code.py
File metadata and controls
65 lines (49 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Copyright 2019 Google LLC
#
# 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
#
# https://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.
from typing import (Callable, Iterable, List, Optional, Tuple, TypeVar)
import itertools
def empty(content: str) -> bool:
"""Return True if this file has no Python statements, False otherwise.
Args:
content (str): A string containing Python code (or a lack thereof).
"""
return not any([i.lstrip() and not i.lstrip().startswith('#')
for i in content.split('\n')])
T = TypeVar('T')
def partition(predicate: Callable[[T], bool],
iterator: Iterable[T]) -> Tuple[List[T], List[T]]:
"""Partitions an iterable into two lists based on a predicate
Args:
predicate (Callable[[T], bool]) : A callable predicate on a single argument
of whatever type is in iterator.
iterator (Iterable(T)): An iterable on any type.
Returns:
Tuple[List[T], List[T]]: The contents of iterator partitioned into two lists.
The first list contains the "true" elements
and the second contains the "false" elements.
"""
results: Tuple[List[T], List[T]] = ([], [])
for i in iterator:
results[int(predicate(i))].append(i)
# Returns trueList, falseList
return results[1], results[0]
def nth(iterable: Iterable[T], n: int, default: Optional[T] = None) -> Optional[T]:
"""Return the nth element of an iterable or a default value.
Args
iterable (Iterable(T)): An iterable on any type.
n (int): The 'index' of the lement to retrieve.
default (Optional(T)): An optional default elemnt if the iterable has
fewer than n elements.
"""
return next(itertools.islice(iterable, n, None), default)