alfraser commited on
Commit
d1f5906
·
1 Parent(s): 53ccfca

Added convenience function to pop N items from the end of a list

Browse files
Files changed (1) hide show
  1. src/common.py +23 -0
src/common.py CHANGED
@@ -2,6 +2,7 @@ import os
2
  import requests
3
  import streamlit as st
4
 
 
5
  from typing import List, Tuple
6
 
7
 
@@ -10,6 +11,28 @@ img_dir = os.path.join(os.path.dirname(__file__), '..', 'img')
10
  config_dir = os.path.join(os.path.dirname(__file__), '..', 'config')
11
 
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  def hf_api_token() -> str:
14
  """
15
  Utility single access point to look up the hugging face access token.
 
2
  import requests
3
  import streamlit as st
4
 
5
+ from random import getrandbits
6
  from typing import List, Tuple
7
 
8
 
 
11
  config_dir = os.path.join(os.path.dirname(__file__), '..', 'config')
12
 
13
 
14
+ def random_true_false() -> bool:
15
+ """
16
+ Utility to get a random True or False to make it clear what I am doing
17
+ and not peppering getrandbits around the codebase
18
+ """
19
+ return bool(getrandbits(1))
20
+
21
+
22
+ def pop_n(items: List, n: int) -> List:
23
+ """
24
+ Utility to pop a certain number from the end of a list.
25
+ Modifies the original list and returns the popped items.
26
+ Automagically pops <n if the list does not contain n items
27
+ """
28
+ if len(items) < n:
29
+ n = len(items)
30
+ popped = []
31
+ for _ in range(n):
32
+ popped.append(items.pop())
33
+ return popped[::-1]
34
+
35
+
36
  def hf_api_token() -> str:
37
  """
38
  Utility single access point to look up the hugging face access token.