pyskills.edit

Text editing tools for modifying files and ipynb notebook cells. Each operation — insert, replace, delete lines, and string(s) replacement — has both a file_* and cell_* variant. All return unified diffs showing what changed, or "none: No changes.", or "error: ...".
from fastcore.test import test_eq
#TODO @llmtool
<unknown>:23: SyntaxWarning: invalid escape sequence '\s'

source

file_edit


def file_edit(
    f, name:NoneType=None
):

Call self as a function.

_tmp = TemporaryDirectory()
_test_path = f'{_tmp.name}/test.txt'
_test_content = 'alpha\nbeta\ngamma\ndelta\n'
def _test_txt(): return Path(_test_path).read_text()
Path(_test_path).write_text(_test_content)
_test_path
'/var/folders/51/b2_szf2945n072c0vj2cyty40000gn/T/tmp2epejvuy/test.txt'

source

insert_line


def insert_line(
    text:str,
    insert_line:int, # The 1-based line number after which to insert the text (0: before 1st line, n>=1: after nth line)
    new_str:str, # The text to insert
):

Insert new_str at specified line number

res = await file_insert_line(_test_path, 0, 'first')
test_eq(_test_txt().splitlines()[0], 'first')
print(res)
@@ -1 +1,2 @@
+first
 alpha

source

str_replace


def str_replace(
    text:str, old_str:str, # Text to find and replace
    new_str:str, # Text to replace with
    start_line:int=None, # Optional 1-based start line to limit search
    end_line:int=None, # Optional 1-based end line to limit search
    n_matches:int=None, # Max replacements (None=all)
    re_filter:str=None, # If provided, only process lines matching this regex (like g// in ex)
    invert_filter:bool=False, # Invert the filter (like g!// in ex)
):

Replace occurrence(s) of old_str with new_str

res = await file_str_replace(_test_path, 'beta', 'BETA')
assert 'BETA' in _test_txt(), f"Expected 'BETA' in file"
print(res)
@@ -2,3 +2,3 @@
 alpha
-beta
+BETA
 gamma

source

strs_replace


def strs_replace(
    text:str, old_strs:list, # List of strings to find and replace
    new_strs:list, # List of replacement strings (must match length of old_strs)
    start_line:int=None, # Optional 1-based start line to limit search
    end_line:int=None, # Optional 1-based end line to limit search
    n_matches:int=None, # Max replacements per string (None=all)
    re_filter:str=None, # If provided, only process lines matching this regex (like g// in ex)
    invert_filter:bool=False, # Invert the filter (like g!// in ex)
):

Replace multiple strings simultaneously

res = await file_strs_replace(_test_path, ['gamma', 'delta'], ['GAMMA', 'DELTA'])
test_eq(_test_txt().splitlines()[-2:], ['GAMMA', 'DELTA'])
print(res)
@@ -3,3 +3,3 @@
 BETA
-gamma
-delta
+GAMMA
+DELTA

source

replace_lines


def replace_lines(
    text:str, start_line:int, # Starting line number to replace (1-based indexing)
    end_line:int=None, # Ending line number to replace (1-based, inclusive, negative counts from end, None for single line)
    new_content:str='', # New content to replace the specified lines
):

Replace line range with new content

res = await file_replace_lines(_test_path, 2, 3, 'two\nthree\n')
test_eq(_test_txt().splitlines()[1:3], ['two', 'three'])
print(res)
@@ -1,4 +1,4 @@
 first
-alpha
-BETA
+two
+three
 GAMMA

source

del_lines


def del_lines(
    text:str, start_line:int, # Starting line number to delete (1-based indexing)
    end_line:int=None, # Ending line number to delete (1-based, inclusive, negative counts from end, None for single line)
    re_filter:str=None, # If provided, only delete lines matching this regex (like g// in ex)
    invert_filter:bool=False, # Invert the filter (like g!// in ex)
):

Delete line range

res = await file_del_lines(_test_path, 1)
test_eq(_test_txt().splitlines()[0], 'two')
print(res)
@@ -1,2 +1 @@
-first
 two
_nb_path = f'{_tmp.name}/test.ipynb'
_tnb = Notebook(new_nb([_test_content, 'other cell']))
_tnb.save(_nb_path)
_cid, _oid = _tnb[0].id, _tnb[1].id
def _nb_src(): return Notebook.open(_nb_path)[_cid].source
_cid, _oid
('8bf920d2', 'aca4e9d0')
res = await cell_insert_line(_nb_path, _cid, 0, 'first')
test_eq(_nb_src().splitlines()[0], 'first')
print(res)
@@ -1 +1,2 @@
+first
 alpha
res = await cell_str_replace(_nb_path, _cid, 'beta', 'BETA')
assert 'BETA' in _nb_src(), f"Expected 'BETA' in cell"
print(res)
@@ -2,3 +2,3 @@
 alpha
-beta
+BETA
 gamma
res = await cell_strs_replace(_nb_path, _cid, ['gamma', 'delta'], ['GAMMA', 'DELTA'])
test_eq(_nb_src().splitlines()[-2:], ['GAMMA', 'DELTA'])
print(res)
@@ -3,3 +3,3 @@
 BETA
-gamma
-delta
+GAMMA
+DELTA
res = await cell_replace_lines(_nb_path, _cid, 2, 3, 'two\nthree\n')
test_eq(_nb_src().splitlines()[1:3], ['two', 'three'])
print(res)
@@ -1,4 +1,4 @@
 first
-alpha
-BETA
+two
+three
 GAMMA
res = await cell_del_lines(_nb_path, _cid, 1)
test_eq(_nb_src().splitlines()[0], 'two')
print(res)
@@ -1,2 +1 @@
-first
 two

source

view_cell


def view_cell(
    fname:str, id:str, nums:bool=True
):

Show cell source with optional line numbers

view_cell displays a cell’s source with line numbers, useful for checking content before editing:

print(view_cell(_nb_path, _cid))
     1 │ two
     2 │ three
     3 │ GAMMA
     4 │ DELTA

source

view_nb


def view_nb(
    fname:str, incl_out:bool=False
):

Show notebook source as concise xml, optionally including output if incl_out

view_nb(_nb_path)
'<nb path="test.ipynb"><code id="8bf920d2">two\nthree\nGAMMA\nDELTA</code><code id="aca4e9d0">other cell</code></nb>'