psu-python 0.2.3__tar.gz → 0.2.4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: psu-python
3
- Version: 0.2.3
3
+ Version: 0.2.4
4
4
  Summary: A python package for abstracting python class methods behind a fully functional layer intended for educational use in CMPSC 131 at The Pennsylvania State University
5
5
  Author-email: Theodore Tasman <psupython@ttasman.com>
6
6
  License-Expression: GPL-3.0
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name='psu-python'
7
- version='0.2.3'
7
+ version='0.2.4'
8
8
  description='A python package for abstracting python class methods behind a fully functional layer intended for educational use in CMPSC 131 at The Pennsylvania State University'
9
9
  authors=[
10
10
  {name='Theodore Tasman', email='psupython@ttasman.com'}
@@ -18,12 +18,13 @@ def delete(key: Any, d: dict) -> Any:
18
18
  Returns:
19
19
  Any: The value of the popped key.
20
20
  """
21
+ # explicitly check type to prevent passing list which has .pop attribute
21
22
  if not isinstance(d, dict):
22
23
  raise TypeError("d must be of type dict")
23
-
24
- value = d.pop(key)
25
24
 
26
- if value is None:
25
+ try:
26
+ value = d.pop(key)
27
+ except KeyError:
27
28
  raise KeyError(f"Key \"{key}\" not in dictionary")
28
29
 
29
30
  return value
@@ -38,10 +39,10 @@ def get_keys(d: dict) -> KeysView:
38
39
  Returns:
39
40
  KeysView: A view of the keys in `d`.
40
41
  """
41
- if not isinstance(d, dict):
42
+ try:
43
+ return d.keys()
44
+ except AttributeError:
42
45
  raise TypeError("d must be of type dict")
43
-
44
- return d.keys()
45
46
 
46
47
 
47
48
  def get_values(d: dict) -> ValuesView:
@@ -53,10 +54,10 @@ def get_values(d: dict) -> ValuesView:
53
54
  Returns:
54
55
  ValuesView: A view of the values in `d`.
55
56
  """
56
- if not isinstance(d, dict):
57
+ try:
58
+ return d.values()
59
+ except AttributeError:
57
60
  raise TypeError("d must be of type dict")
58
-
59
- return d.values()
60
61
 
61
62
 
62
63
  def get_items(d: dict) -> ItemsView:
@@ -68,10 +69,11 @@ def get_items(d: dict) -> ItemsView:
68
69
  Returns:
69
70
  ValuesView: A view of the (key, value) tuple pairs in `d`.
70
71
  """
71
- if not isinstance(d, dict):
72
+ try:
73
+ return d.items()
74
+ except AttributeError:
72
75
  raise TypeError("d must be of type dict")
73
-
74
- return d.items()
76
+
75
77
 
76
78
  # get key from d
77
79
  def get_value(key: Any, d: dict, default: Any = None) -> Any:
@@ -85,7 +87,7 @@ def get_value(key: Any, d: dict, default: Any = None) -> Any:
85
87
  Returns:
86
88
  Any: The value corresponding to `key`, or `default` if not in `d`.
87
89
  """
88
- if not isinstance(d, dict):
89
- raise TypeError("d must be of type dict")
90
-
91
- return d.get(key, default)
90
+ try:
91
+ return d.get(key, default)
92
+ except AttributeError:
93
+ raise TypeError("d must be of type dict")
@@ -1,6 +1,7 @@
1
1
  from typing import Any, Optional
2
2
 
3
- # "join list by separator"
3
+ # "join list by separator"
4
+ # should really take any iterable, not specifically a list
4
5
  def join(lst: list, separator: str) -> str:
5
6
  """Join elements of a list into a single string, separated by a delimiter.
6
7
 
@@ -11,12 +12,13 @@ def join(lst: list, separator: str) -> str:
11
12
  Returns:
12
13
  str: a joined string of the list elements
13
14
  """
14
- if not isinstance(separator, str):
15
+ try:
16
+ return separator.join(lst)
17
+ except AttributeError:
15
18
  raise TypeError("separator must be of type str")
16
- if not isinstance(lst, list):
19
+ except TypeError:
17
20
  raise TypeError("lst must be of type list")
18
21
 
19
- return separator.join(lst)
20
22
 
21
23
  # "append element to list"
22
24
  def append(element: Any, lst: list) -> None:
@@ -26,9 +28,11 @@ def append(element: Any, lst: list) -> None:
26
28
  lst (list): the list to append to
27
29
  element (Any): the item to append
28
30
  """
29
- if not isinstance(lst, list):
31
+ try:
32
+ lst.append(element)
33
+ except AttributeError:
30
34
  raise TypeError("lst must be of type list")
31
- lst.append(element)
35
+
32
36
 
33
37
  # "pop list[index]"
34
38
  def pop(lst: list, index: Optional[int] = None) -> Any:
@@ -44,15 +48,13 @@ def pop(lst: list, index: Optional[int] = None) -> Any:
44
48
  Returns:
45
49
  Any: The popped element.
46
50
  """
51
+ # explicitly check type to prevent passing dict which has .pop attribute
47
52
  if not isinstance(lst, list):
48
53
  raise TypeError("lst must be of type list")
49
- if not isinstance(index, int) and index is not None:
50
- raise TypeError("index must be None or of type int")
51
54
 
52
- if index is not None and (index > len(lst) - 1 or -1 * index > len(lst)):
55
+ try:
56
+ return lst.pop() if index is None else lst.pop(index)
57
+ except IndexError:
53
58
  raise IndexError("pop index out of range")
54
-
55
- if index is None:
56
- return lst.pop()
57
-
58
- return lst.pop(index)
59
+ except TypeError:
60
+ raise TypeError("index must be None or of type int")
@@ -0,0 +1,42 @@
1
+ from typing import Optional
2
+
3
+ # split string by sep
4
+ def split(string: str, sep: Optional[str] = None, maxsplit: int = -1) -> list[str]:
5
+ """Create a list of words in `string` separated by `sep`.
6
+
7
+ Args:
8
+ string (str): The string to split.
9
+ separator (str, optional): The separator delimiting words. If not specified or `None`, splits at whitespace.
10
+ maxsplit (int, optional): The maximum number of splits to complete. If not specified or `-1`, all possible splits are made.
11
+
12
+ Returns:
13
+ list: A list containing the words after splitting.
14
+ """
15
+ # explicitly check argument types since they cannot be differentiated via duck typing (both TypeError)
16
+ if sep is not None and not isinstance(sep, str):
17
+ raise TypeError("separator must be of type str")
18
+ if not isinstance(maxsplit, int):
19
+ raise TypeError("maxsplit must be of type int")
20
+
21
+ try:
22
+ return string.split(sep, maxsplit)
23
+ except AttributeError as exc:
24
+ raise TypeError("string must be of type str") from exc
25
+
26
+
27
+ def strip(string: str, chars: Optional[str] = None) -> str:
28
+ """Remove characters in `chars` from beginning and end of `string`.
29
+
30
+ Args:
31
+ string (str): the string to strip.
32
+ chars (str, optional): string of characters to strip. If not specified or `None`, strips whitespace.
33
+
34
+ Returns:
35
+ str: a new string with characters stripped.
36
+ """
37
+ try:
38
+ return string.strip(chars)
39
+ except AttributeError:
40
+ raise TypeError("string must be of type str")
41
+ except TypeError:
42
+ raise TypeError("chars must be None or of type str")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: psu-python
3
- Version: 0.2.3
3
+ Version: 0.2.4
4
4
  Summary: A python package for abstracting python class methods behind a fully functional layer intended for educational use in CMPSC 131 at The Pennsylvania State University
5
5
  Author-email: Theodore Tasman <psupython@ttasman.com>
6
6
  License-Expression: GPL-3.0
@@ -1,20 +0,0 @@
1
- from typing import Optional
2
-
3
- # split string by sep
4
- def split(string: str, sep: Optional[str] = None, maxsplit: int = -1) -> list[str]:
5
- """Create a list of words in `string` separated by `sep`.
6
-
7
- Args:
8
- string (str): The string to split.
9
- separator (str, optional): The separator delimiting words. If not specified or `None`, splits at whitespace.
10
- maxsplit (int, optional): The maximum number of splits to complete. If not specified or `-1`, all possible splits are made.
11
-
12
- Returns:
13
- list: A list containing the words after splitting.
14
- """
15
- if not isinstance(string, str):
16
- raise TypeError("string must be of type str")
17
- if not isinstance(sep, str) and sep is not None:
18
- raise TypeError("separator must be of type str")
19
-
20
- return string.split(sep, maxsplit)
File without changes
File without changes
File without changes