Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit aa5f1e4

Browse files
author
Matthias Koeppe
committed
src/sage/misc/replace_dot_all.py: Clean docstrings, uncamelcaps
1 parent 8cef447 commit aa5f1e4

File tree

1 file changed

+14
-23
lines changed

1 file changed

+14
-23
lines changed

src/sage/misc/replace_dot_all.py

+14-23
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def parse_arguments():
9696

9797
def find_replacements(location, package_regex=None, verbose=False):
9898
r"""
99-
Locates the lines in the file at location which match the regex pattern.
99+
Locate the lines in the file at ``location`` which contain an ``import`` statement.
100100
101101
INPUT:
102102
@@ -350,14 +350,13 @@ def process_line(location, line, replacements, import_index, verbose=False):
350350
# to make all replacements matching the regex in a single file with filepath "location"
351351

352352

353-
def make_replacements_in_file(location, package_regex, verbose=False, output=None):
353+
def make_replacements_in_file(location, package_regex=None, verbose=False, output=None):
354354
r"""
355-
Writes over the file with filepath "location" making replacements for lines matching the regex pattern: 'from\s+sage(|[.](arith|categories|combinat|ext|graphs(|[.]decompositions)|interfaces|libs|matrix|misc|numerical(|[.]backends)|rings|sets))[.]all\s+import'
355+
Replace ``import`` statements in the file with filepath "location".
356356
357357
INPUT:
358358
359-
- ``location`` -- a file path file_to_change = 'schemes/elliptic_curves/ell_rational_field.py'
360-
location = cwd + file_to_change
359+
- ``location`` -- a file path
361360
- ``package_regex`` -- (default: :obj:`default_package_regex`) a regular expression matching
362361
the ``sage.PAC.KAGE.all`` package names from which we do not want to import.
363362
- ``verbose`` -- if True, issue print statements when interesting examples are found
@@ -394,33 +393,29 @@ def make_replacements_in_file(location, package_regex, verbose=False, output=Non
394393
# to iterate over all files in src/sage matching the given regular expression
395394

396395

397-
def walkdir_replace_dot_all(dir, fileRegex, package_regex, verbose=False):
396+
def walkdir_replace_dot_all(dir, file_regex=r'.*[.](py|pyx|pxi)$', package_regex=None, verbose=False):
398397
r"""
399-
Writes over the files in src/sage matching the regex pattern fileRegex making replacements to all lines in such files
400-
which match the regex pattern: 'from\s+sage(|[.](arith|categories|combinat|ext|graphs(|[.]decompositions)|interfaces|libs|matrix|misc|numerical(|[.]backends)|rings|sets))[.]all\s+import'
398+
Replace ``import`` statements in the files in directory ``dir`` matching the regex pattern ``file_regex``.
401399
402400
INPUTS:
403401
404-
- ``fileRegex`` -- a regular expression locating strings containing certain module.all import statements. The suggested value is ``fileRegex = r'.*[.](py|pyx|pxi)$'``.
402+
- ``dir`` -- a directory path
403+
- ``file_regex`` -- a regular expression matching the file names to process
405404
- ``package_regex`` -- (default: :obj:`default_package_regex`) a regular expression matching
406405
the ``sage.PAC.KAGE.all`` package names from which we do not want to import.
407-
- ``verbose`` -- a parameter which if used will issue print statements when interesting examples are found
406+
- ``verbose`` -- if True, print statements when interesting examples are found
408407
409408
EXAMPLES::
410409
411410
sage: from sage.misc.replace_dot_all import *
412-
sage: os.chdir(sage.env.SAGE_SRC + '/sage') # change to sage directory
413-
sage: dir = os.getcwd() # Get the current working directory
414-
sage: fileRegex = r'.*[.](py|pyx|pxi)$'
415-
sage: regex = r"from\s+sage(|[.](arith|categories|combinat|ext|graphs(|[.]decompositions)|interfaces|libs|matrix|misc|numerical(|[.]backends)|rings|sets))[.]all\s+import"
416-
sage: walkdir_replace_dot_all(dir, fileRegex, regex)
411+
sage: walkdir_replace_dot_all(sage.env.SAGE_SRC + '/sage') # not tested
417412
"""
418413
global numberFiles, numberFilesMatchingRegex
419-
pattern = re.compile(fileRegex)
414+
file_regex = re.compile(file_regex)
420415
for root, dirs, files in os.walk(dir, topdown=False):
421416
for name in files:
422417
numberFiles += 1
423-
if pattern.search(name):
418+
if file_regex.search(name):
424419
numberFilesMatchingRegex += 1
425420
location = os.path.join(root, name)
426421
if location.find('replace_dot_all') == -1: # to avoid changing anything in this file itself
@@ -449,12 +444,8 @@ def print_log_messages():
449444
# Parse the arguments
450445
args = parse_arguments()
451446
verbosity = args.verbose
452-
# Print arguments
453-
print("Executing replace_dot_all.py with arguments:")
454-
for a in args.__dict__:
455-
print(' ' + str(a) + ": " + str(args.__dict__[a]))
456447
# Declare regular expressions
457-
fileRegex = r'.*[.](py|pyx|pxi)$'
448+
file_regex = r'.*[.](py|pyx|pxi)$'
458449
package_regex = None
459450
# Execute the main function based on the specified location and verbosity
460451
if not args.location:
@@ -463,7 +454,7 @@ def print_log_messages():
463454
for location in args.location:
464455
if not (location.endswith('.py') or location.endswith('.pxi')):
465456
# Assume directory
466-
walkdir_replace_dot_all(location, fileRegex, package_regex, verbose=verbosity)
457+
walkdir_replace_dot_all(location, file_regex, package_regex, verbose=verbosity)
467458
else:
468459
# make replacements in file specified by location argument
469460
make_replacements_in_file(location, package_regex, verbose=verbosity)

0 commit comments

Comments
 (0)