Use json.loads() to convert it back to a Python list of lists.
User
how about passing list of list of strings
Assistant
Same approach works for list of list of strings — just pass a JSON string and parse it with json.loads().
Example Python script (script.py):
python
import argparse
import json
parser = argparse.ArgumentParser()
parser.add_argument('--data', type=str, required=True, help='List of list of strings in JSON format')
args = parser.parse_args()
list_of_lists = json.loads(args.data)
print(list_of_lists)
Run from bash:
bash
python script.py --data '[["a","b"],["c","d"]]'
Output:
code
[['a', 'b'], ['c', 'd']]
Note: Always use double quotes " inside the JSON string and single quotes ' around the whole argument in bash to avoid quoting issues.
User
I have this:
modules_to_hook="language_model.model.layers.30,"language_model.model.layers.${i}" "
and this:
parser.add_argument(
"--modules_to_hook",
type=parse_list_of_lists,
default=None,
help='A list of lists. Example format: "1,2,3;4,5,6". Contains the lists of modules to hook for each hook.',
)
How to fix it
Assistant
You want to pass a list of lists of module names as a string argument, e.g.:
Define parse_list_of_lists in Python to parse this format:
python
def parse_list_of_lists(s):
# Split by ';' to get each list
outer = s.split(';')
# Split each inner list by ','
return [item.split(',') for item in outer]
Use argparse:
python
parser.add_argument(
"--modules_to_hook",
type=parse_list_of_lists,
default=None,
help='A list of lists. Example format: "1,2,3;4,5,6".'
)