Skip to main content
Code Review

Return to Question

deleted 24 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

python: propagate Propagate a default parameter of a function among several files

I am starting to write a CLI using argparse library. This is my first attempt to structure a large project, usually I write single script. The CLI auto is defined in a top directory, helper functions are in a subfolder named project and a main function (where I want to put the logic of my program) in sub-sub-folder named sync.

Here is the tree-structure:

├── top/
├── auto
├── __init__.py
 └── project/
 ├── helper.py
 ├── __init__.py
 └── sync/
 ├── __init__.py
 ├── sync_file.py
├── top/
├── auto
├── __init__.py
 └── project/
 ├── helper.py
 ├── __init__.py
 └── sync/
 ├── __init__.py
 ├── sync_file.py

helper.py code:helper.py:

def play(device, verbose=False):
 if verbose:
 print device + "with verbose on"
 else:
 print device + "verbose off"

sync_file.py code:sync_file.py:

from .. import helper
def main(device, verbose=False):
 helper.play(device,verbose)
if __name__ == '__main__':
 #Test here the logic 
 main('omv')
 main('omv',verbose=True)

auto code:auto:

#!/usr/bin/env python
import argparse
import project.sync.sync_file as logic_sync
def sync(args):
 if args.verbose:
 logic_sync.main(args.device,verbose=True)
 else:
 logic_sync.main(args.device)
parser = argparse.ArgumentParser(description='Info on my project')
subparsers = parser.add_subparsers()
sync_parser = subparsers.add_parser('sync', help='synchronize')
sync_parser.add_argument('--device', default='nas', help='specify the device name')
sync_parser.add_argument('--verbose', action='store_true', help='increase the output verbose')
sync_parser.set_defaults(func=sync) # set the default function to sync
if __name__ == '__main__':
 args = parser.parse_args()
 args.func(args)

After I've made the auto file executable chmode +x auto I can use the CLI:

./auto sync
# nasverbose off
./auto sync --verbose
# naswith verbose on

I would like to keep this structure but I see a lot of repetition in propagating the verbose parameter from one file to another, is. Is there a better way to do it? thanks

python: propagate a default parameter of a function among several files

I am starting to write a CLI using argparse library. This is my first attempt to structure a large project, usually I write single script. The CLI auto is defined in a top directory, helper functions are in a subfolder named project and a main function (where I want to put the logic of my program) in sub-sub-folder named sync. Here is the tree-structure:

├── top/
├── auto
├── __init__.py
 └── project/
 ├── helper.py
 ├── __init__.py
 └── sync/
 ├── __init__.py
 ├── sync_file.py

helper.py code:

def play(device, verbose=False):
 if verbose:
 print device + "with verbose on"
 else:
 print device + "verbose off"

sync_file.py code:

from .. import helper
def main(device, verbose=False):
 helper.play(device,verbose)
if __name__ == '__main__':
 #Test here the logic 
 main('omv')
 main('omv',verbose=True)

auto code:

#!/usr/bin/env python
import argparse
import project.sync.sync_file as logic_sync
def sync(args):
 if args.verbose:
 logic_sync.main(args.device,verbose=True)
 else:
 logic_sync.main(args.device)
parser = argparse.ArgumentParser(description='Info on my project')
subparsers = parser.add_subparsers()
sync_parser = subparsers.add_parser('sync', help='synchronize')
sync_parser.add_argument('--device', default='nas', help='specify the device name')
sync_parser.add_argument('--verbose', action='store_true', help='increase the output verbose')
sync_parser.set_defaults(func=sync) # set the default function to sync
if __name__ == '__main__':
 args = parser.parse_args()
 args.func(args)

After I've made the auto file executable chmode +x auto I can use the CLI:

./auto sync
# nasverbose off
./auto sync --verbose
# naswith verbose on

I would like to keep this structure but I see a lot of repetition in propagating the verbose parameter from one file to another, is there a better way to do it? thanks

Propagate a default parameter of a function among several files

I am starting to write a CLI using argparse library. This is my first attempt to structure a large project, usually I write single script. The CLI auto is defined in a top directory, helper functions are in a subfolder named project and a main function (where I want to put the logic of my program) in sub-sub-folder named sync.

Here is the tree-structure:

├── top/
├── auto
├── __init__.py
 └── project/
 ├── helper.py
 ├── __init__.py
 └── sync/
 ├── __init__.py
 ├── sync_file.py

helper.py:

def play(device, verbose=False):
 if verbose:
 print device + "with verbose on"
 else:
 print device + "verbose off"

sync_file.py:

from .. import helper
def main(device, verbose=False):
 helper.play(device,verbose)
if __name__ == '__main__':
 #Test here the logic 
 main('omv')
 main('omv',verbose=True)

auto:

#!/usr/bin/env python
import argparse
import project.sync.sync_file as logic_sync
def sync(args):
 if args.verbose:
 logic_sync.main(args.device,verbose=True)
 else:
 logic_sync.main(args.device)
parser = argparse.ArgumentParser(description='Info on my project')
subparsers = parser.add_subparsers()
sync_parser = subparsers.add_parser('sync', help='synchronize')
sync_parser.add_argument('--device', default='nas', help='specify the device name')
sync_parser.add_argument('--verbose', action='store_true', help='increase the output verbose')
sync_parser.set_defaults(func=sync) # set the default function to sync
if __name__ == '__main__':
 args = parser.parse_args()
 args.func(args)

After I've made the auto file executable chmode +x auto I can use the CLI:

./auto sync
# nasverbose off
./auto sync --verbose
# naswith verbose on

I would like to keep this structure but I see a lot of repetition in propagating the verbose parameter from one file to another. Is there a better way to do it?

Source Link
diegus
  • 229
  • 2
  • 7

python: propagate a default parameter of a function among several files

I am starting to write a CLI using argparse library. This is my first attempt to structure a large project, usually I write single script. The CLI auto is defined in a top directory, helper functions are in a subfolder named project and a main function (where I want to put the logic of my program) in sub-sub-folder named sync. Here is the tree-structure:

├── top/
├── auto
├── __init__.py
 └── project/
 ├── helper.py
 ├── __init__.py
 └── sync/
 ├── __init__.py
 ├── sync_file.py

helper.py code:

def play(device, verbose=False):
 if verbose:
 print device + "with verbose on"
 else:
 print device + "verbose off"

sync_file.py code:

from .. import helper
def main(device, verbose=False):
 helper.play(device,verbose)
if __name__ == '__main__':
 #Test here the logic 
 main('omv')
 main('omv',verbose=True)

auto code:

#!/usr/bin/env python
import argparse
import project.sync.sync_file as logic_sync
def sync(args):
 if args.verbose:
 logic_sync.main(args.device,verbose=True)
 else:
 logic_sync.main(args.device)
parser = argparse.ArgumentParser(description='Info on my project')
subparsers = parser.add_subparsers()
sync_parser = subparsers.add_parser('sync', help='synchronize')
sync_parser.add_argument('--device', default='nas', help='specify the device name')
sync_parser.add_argument('--verbose', action='store_true', help='increase the output verbose')
sync_parser.set_defaults(func=sync) # set the default function to sync
if __name__ == '__main__':
 args = parser.parse_args()
 args.func(args)

After I've made the auto file executable chmode +x auto I can use the CLI:

./auto sync
# nasverbose off
./auto sync --verbose
# naswith verbose on

I would like to keep this structure but I see a lot of repetition in propagating the verbose parameter from one file to another, is there a better way to do it? thanks

lang-py

AltStyle によって変換されたページ (->オリジナル) /