-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patho1-prompt.txt
106 lines (73 loc) · 5.25 KB
/
o1-prompt.txt
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
### FractionalIndex
A `FractionalIndex` takes a list of existing version strings and allows you to insert a new version between any two existing versions, or at the start or end. It is used by FastMigrate, for instance, to generate file names.
Internally, it uses the `fractional_indexing` pypi package, which works as follows:
```python
first = generate_key_between(None, None) # 'a0'
second = generate_key_between(first, None) # 'a1' (after 1st)
third = generate_key_between(second, None) # 'a2' (after 2nd)
zeroth = generate_key_between(None, first) # 'Zz' (before 1st)
```
`FractionalIndex` differs with `fractional_indexing` in how it handles skipping the `before` or `after` version parameters. If you only pass `before`, it will insert before the specified version, but after the largest version before that (if there is none, then it will insert at the start). If you only pass `after`, it will insert after the specified version, but before the smallest version after that (if there is none, then it will insert at the end).
```python
idx = FractionalIndex()
i1 = idx.insert() # starts a new index
print(i1) # 'a0'
i2 = idx.insert(after=i1) # inserts after 1st
i3 = idx.insert(before=i2) # inserts before 2nd
i4 = idx.insert(i3, i2) # inserts between i3 and i2
i5 = idx.insert() # adds to the end
```
To add to the start, you can either pass the first version to `insert(before=...)`, or use `begin()`:
```python
i6 = idx.begin()
```
You can create an index from a list of existing versions, which will be sorted:
```python
idx = FractionalIndex([i1, i2, i3, i4, i5, i6])
```
### Implementation
Internally, `FractionalIndex` by default uses a subclass of `sortedcontainers.SortedList` to store the versions. The sort is needed so that `insert` without one of both of `after` or `before` can quickly find the next, previous or start/end item. The subclass, IndexingList, adds the following methods (which are the only methods required by `FractionalIndex`):
- `after(item)`: returns the next item after the specified item, or `None` if there is none.
- `before(item)`: returns the previous item before the specified item, or `None` if there is none.
- `begin()`: returns the first item.
- `end()`: returns the last item.
### Alternative Implementations
FractionalIndex comes with a few alternative implementations.
#### FileIndex
FileIndex is a simple implementation which uses a list of files in a directory, and a version separator, to generate the index. For instance, consider the following directory contents:
```
a0-create-table.sql
a1-add-column.sql
a2-add-index.sql
```
In this case, we can create a suitable `FileIndex` with `idx = FileIndex(directory='.', separator='-')`. Since these are the default values, we can simply call `FileIndex()` to create it.
Note that when calling after/before/begin/end by default the directory will be re-scanned for the latest files. If you want to avoid this, you can pass `scan=False` to the constructor.
#### SqlIndex
`SqliteIndex` indexes a sqlite DB table. Generally, the fractional index will be the primary key of the table. To use it, you need to pass a `Connection`, `table` and `column` (defaults to 'id') to the constructor:
```python
idx = SqliteIndex(conn, 'table', 'column')
```
For instance, `after(item)` is implemented as:
```python
fetchone(conn, f"SELECT min({column}) FROM {table} WHERE {column}>?", item)
```
...and `begin()` is implemented as:
```python
fetchone(conn, f"SELECT min({column}) FROM {table}")
```
...where `fetchone` is a helper function which executes a query:
```python
def fetchone(conn, query, *params):
res = conn.execute(query, params).fetchone()
return res[0] if res else None
```
----
Above is the README for a proposed new python library. Implement the library, as follows:
- Your response should be a single fenced markdown block, which should not included any triple-backticks inside the block -- i.e. other than the start/end of the fence containing your reply
- Your entire response will be pasted into bash, so it should include `mkdir -p` to create any necessary directories
- For creating files, use the `cat > filename.py << EOF` pattern
- Follow Jeremy Howard's fastai coding style, which uses concise mnemonic variable names, single-line conditionals/loops if the body is single-line, single-line ternary ops, but keeps things readable and clear. Do not include comments unless calling out unexpected behavior or complex algorithmic issues
- Include a pyproject.toml. The project will use regular pip installation without any additional setup deps like hatchling
- Use pytest for tests. Each test should be compact and concise, and all key functionality should be tested
- Error handling should be practical, not over-the-top -- this is code designed to be read, not to put man on the moon!
After pasting your response into bash, the result should be ready to pass all tests and then use twine to upload to pypi. You do not need to create a README -- we will use the text above to create that file. You do not need to create LICENSE etc files, we will copy those from other projects. If there are any other requirements to complete testing, running, and packaging the lib, and/or you needed to make any assumptions you like to document, you can have your response create a `NOTES.txt` file with any notes you wish to provide.