pyskills.edit

Functions for creating, viewing, and modifying files. Each editing operation returns unified diffs showing what changed.
from fastcore.test import test_eq
<unknown>:16: SyntaxWarning: invalid escape sequence '\s'

source

file_view


def file_view(
    path:str, # Path to view (expands `~` if needed)
    startline:int=1, # Starting line to view
    endline:int=None, # End line (defaults to last line if None)
):

Read file contents, optionally limited to 1-based line range

_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/tmpmgzy8axc/test.txt'
print(file_view(_test_path, 2, 30))
2: beta
3: gamma
4: delta

source

file_create


def file_create(
    path:str, # Path to create (expands `~` if needed)
    contents:str, # Contents of file to create
):

Create a new file with contents. Error if file exists.

_new = Path(_tmp.name)/'demo.txt'
print(file_create(_new, 'one\ntwo\nthree\n'))
@@ -0,0 +1,3 @@
+one
+two
+three

source

file_edit


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

Call self as a function.


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 = 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 = 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 = 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 = 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