Wrapper for Python's argparse classPathname matching and listing programA wrapper class for Sessions in PHPPython Decorator - inspecting function argument valuesWindow Creation Wrapper ClassWrapper class for PHPMailerUsing argparse module within cmd interfacePHP MySQLi wrapper classWrapper around Python's poplib.POP3 and poplib.POP3_SSLObject for generating HTML for hyperlinks

Drawing Realistic Linux Command Shell Windows with tcolorbox

What was Jeremy Corbyn’s involvement in the Northern Ireland peace process?

Apps are not allowed by Google to read SMS messages, but why can they read OTP SMS directly?

Is an afterburner louder than the same jet engine without it?

In the sentence "der hatte doch eine Brille", why do we use 'der' instead of 'er'?

Unstable manifolds of a Morse function give a CW complex

Plane ticket price went down by 40% two weeks after I booked it. Is there anything I can do to get a refund?

What was the first operating system called DOS?

How many assistant professor or postdoc jobs do people usually apply for in mathematics?

Not registering my US born child of 1 US parent as a US citizen

Is a manifold paracompact? Should it be?

How to use zgrep to find out what line number or give some contextual info. surrounding a .gz file

Why is Gaia operating around Earth orbit? Why not send it to Neptune's orbit?

Password generator in python

How to create electric light with 1300s technology

Old story about a man with tattoos that told stories

Cheap and safe way to dim 100+ 60W Incandescent bulbs

Why the translation is not linear transformation?

What's the best way of typing the following 58 equations into LaTeX?

What mathematics activities get students physically moving?

Do solvers use GUB/SOS1 branching?

Why didn't Philippine Airlines Flight 113 dump fuel prior to forced landing?

Implementing solvers with Object Oriented Programming

4 Attempts to Guess a Number Between 1-15



Wrapper for Python's argparse class


Pathname matching and listing programA wrapper class for Sessions in PHPPython Decorator - inspecting function argument valuesWindow Creation Wrapper ClassWrapper class for PHPMailerUsing argparse module within cmd interfacePHP MySQLi wrapper classWrapper around Python's poplib.POP3 and poplib.POP3_SSLObject for generating HTML for hyperlinks






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









5















$begingroup$


I'm just trying to learn python: I tend to write my own customized wrappers arround "more complicated" python functionalities so that i can use them more easily, more consistent, easier to change in one place, etc.



I.e. if wrote my own class my_os with i.e. has the functions rm(path) or mv(from_path, to_path) so that on application- or script-level I don't need to know or care (or remember ;)) about the specialities of os , shutil, pathlib or whatever.



I did the same for argparse. Even though my own functions are quite sparse in this case, I still prefer a named my_add_required_arg over worrying about the action parameter of the native function in every script I write.



So much to the history of this class:



#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""Wrapper functionality for Pythons built-in argparse package."""

import argparse


class myArgparser(argparse.ArgumentParser):
"""Offers a wrapper for Pythons built-in argparse package.

It offers simple named functions to

* add a brief description
* set a version
* set optional named arguments
* set non optional named arguments
* set optional flags

and then to return the parsed arguments.
"""

def __init__(self, description=None):
"""Create the main argparser object.

Args:
description: The optional description.
"""
super(myArgparser, self).__init__(description=description)

# required arguments is just printed out on "-h"
self.myRequired = self.add_argument_group("required arguments")

def my_set_version(self, version):
"""Set the version information, printed out by --version.

Args:
version: version string
"""
self.add_argument("--version", action="version", version=version)

def my_add_flag(self, short, long, help):
"""Add a boolean flag.

If it is set, the flag argument is set to True, else to False.

Args:
short: the short name, i.e. '-d'
long: the long name, i.e. '--debug'
help: description printed out on '-h'
"""
self.add_argument(short, long, help=help, action="store_true")

def my_add_arg(self, short, long, help):
"""Add an optional named argument of string type.

If it isn't set on the commandline, it defaults to None.

Args:
short: the short name, i.e. '-ld'
long: the long name, i.e. '--logdir'
help: description printed out on '-h'
"""
self.add_argument(short, long, help=help, action="store")

def my_add_required_arg(self, short, long, help):
"""Add an required named argument of string Type.

Args:
short: the short name, i.e. '-ld'
long: the long name, i.e. '--logdir'
help: description printed out on '-h'
"""
self.myRequired.add_argument(
short, long, help=help, action="store", required=True
)

def my_get_args(self):
"""Return the arguments depending on the configured and set arguments.

Return:
Namespace-Object containing values for all added arguments
"""
return self.parse_args()


But using this class, I realized that I was using the same functions with oftenly the same parameters at the beginning of each of my scripts which needed arguments, so i added another function:



@classmethod
def get_args(
cls,
description=None,
version=None,
optional_args=[],
required_args=[],
flags=[],
):
"""Return the parsed arguments.

Each passed argument or flag tuple must contain the short name, the long name and the description
of the argument.

Args:
description: the optional description
version: the optional version string
optional_args: as list of tuples
required_args: as list of tuples
flags: as list of tuples
"""
argparser = cls(description)

if version is not None:
argparser.my_set_version(version)

for arg in optional_args:
argparser.my_add_arg(*arg)

for arg in required_args:
argparser.my_add_required_arg(*arg)

for flag in flags:
argparser.my_add_flag(*flag)

return argparser.my_get_args()



So now I can start a script with a clean argument definition, which I like:



# set global vars & args
version = 'mySpeedtest-20190918'
description = "Test the actual internet connection speed and log the result"
optional_args = [('-ld','--logdir', 'set destination directory for the logfile')]
flags = [('-d', '--debug', 'set loglevel to debug')]

args = my_util.my_argparse.myArgparser.get_args(description, version, optional_args, flags=flags)


Furthermore I don't have bloated init-function and still could use the single functions if I allready strongly had or wanted to.



Now the question: Does it make sense/is it good practice/is it "pythonic" to use a @classmethod as a pseudo-constructer like this?



(Kind of "Because you can do it, doesn't mean necessarily you should do it!"-Question ;))










share|improve this question











$endgroup$










  • 1




    $begingroup$
    Your question "Is it good practice to use a @classmethod as a pseudo-constructer ? " is a question that I feel belongs more to Software Engineering. In general Code Review will give you more advices about how to structure implementation, while SE helps more structuring interfaces.
    $endgroup$
    – Arthur Havlicek
    Sep 19 at 9:38











  • $begingroup$
    @ArthurHavlicek I guess after reading Gloweyes response, my question might not be as desired, but the response is a perfect kind of code review :)
    $endgroup$
    – evilive
    Sep 19 at 11:10










  • $begingroup$
    Just to be clear, I didn't mean that your question is offtopic there because you did post code to be reviewed and it's acceptable scopes of stack exchanges overlap a bit but if you did want a theoritical, elaborate point of view about structuring your classes API, with pro/cons of alternatives based on your requirements, then SE was more likely to give that. I actually would be surprised you don't already find good practices questions about classmethods there, if you are interested.
    $endgroup$
    – Arthur Havlicek
    Sep 19 at 11:38











  • $begingroup$
    Have you ever used the docopt library? You just write the help docstring and it automatically figures out everything from that.
    $endgroup$
    – Reinstate Monica
    Sep 19 at 17:49










  • $begingroup$
    @Justin No I didn'i know it yet, thank you. Allthough it reads nice I don't think that I'm gonna use it: The helptext and parameter description printed out with -h for the user mustn't necessarily be same information as the docstring for the developer.
    $endgroup$
    – evilive
    Sep 20 at 5:31

















5















$begingroup$


I'm just trying to learn python: I tend to write my own customized wrappers arround "more complicated" python functionalities so that i can use them more easily, more consistent, easier to change in one place, etc.



I.e. if wrote my own class my_os with i.e. has the functions rm(path) or mv(from_path, to_path) so that on application- or script-level I don't need to know or care (or remember ;)) about the specialities of os , shutil, pathlib or whatever.



I did the same for argparse. Even though my own functions are quite sparse in this case, I still prefer a named my_add_required_arg over worrying about the action parameter of the native function in every script I write.



So much to the history of this class:



#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""Wrapper functionality for Pythons built-in argparse package."""

import argparse


class myArgparser(argparse.ArgumentParser):
"""Offers a wrapper for Pythons built-in argparse package.

It offers simple named functions to

* add a brief description
* set a version
* set optional named arguments
* set non optional named arguments
* set optional flags

and then to return the parsed arguments.
"""

def __init__(self, description=None):
"""Create the main argparser object.

Args:
description: The optional description.
"""
super(myArgparser, self).__init__(description=description)

# required arguments is just printed out on "-h"
self.myRequired = self.add_argument_group("required arguments")

def my_set_version(self, version):
"""Set the version information, printed out by --version.

Args:
version: version string
"""
self.add_argument("--version", action="version", version=version)

def my_add_flag(self, short, long, help):
"""Add a boolean flag.

If it is set, the flag argument is set to True, else to False.

Args:
short: the short name, i.e. '-d'
long: the long name, i.e. '--debug'
help: description printed out on '-h'
"""
self.add_argument(short, long, help=help, action="store_true")

def my_add_arg(self, short, long, help):
"""Add an optional named argument of string type.

If it isn't set on the commandline, it defaults to None.

Args:
short: the short name, i.e. '-ld'
long: the long name, i.e. '--logdir'
help: description printed out on '-h'
"""
self.add_argument(short, long, help=help, action="store")

def my_add_required_arg(self, short, long, help):
"""Add an required named argument of string Type.

Args:
short: the short name, i.e. '-ld'
long: the long name, i.e. '--logdir'
help: description printed out on '-h'
"""
self.myRequired.add_argument(
short, long, help=help, action="store", required=True
)

def my_get_args(self):
"""Return the arguments depending on the configured and set arguments.

Return:
Namespace-Object containing values for all added arguments
"""
return self.parse_args()


But using this class, I realized that I was using the same functions with oftenly the same parameters at the beginning of each of my scripts which needed arguments, so i added another function:



@classmethod
def get_args(
cls,
description=None,
version=None,
optional_args=[],
required_args=[],
flags=[],
):
"""Return the parsed arguments.

Each passed argument or flag tuple must contain the short name, the long name and the description
of the argument.

Args:
description: the optional description
version: the optional version string
optional_args: as list of tuples
required_args: as list of tuples
flags: as list of tuples
"""
argparser = cls(description)

if version is not None:
argparser.my_set_version(version)

for arg in optional_args:
argparser.my_add_arg(*arg)

for arg in required_args:
argparser.my_add_required_arg(*arg)

for flag in flags:
argparser.my_add_flag(*flag)

return argparser.my_get_args()



So now I can start a script with a clean argument definition, which I like:



# set global vars & args
version = 'mySpeedtest-20190918'
description = "Test the actual internet connection speed and log the result"
optional_args = [('-ld','--logdir', 'set destination directory for the logfile')]
flags = [('-d', '--debug', 'set loglevel to debug')]

args = my_util.my_argparse.myArgparser.get_args(description, version, optional_args, flags=flags)


Furthermore I don't have bloated init-function and still could use the single functions if I allready strongly had or wanted to.



Now the question: Does it make sense/is it good practice/is it "pythonic" to use a @classmethod as a pseudo-constructer like this?



(Kind of "Because you can do it, doesn't mean necessarily you should do it!"-Question ;))










share|improve this question











$endgroup$










  • 1




    $begingroup$
    Your question "Is it good practice to use a @classmethod as a pseudo-constructer ? " is a question that I feel belongs more to Software Engineering. In general Code Review will give you more advices about how to structure implementation, while SE helps more structuring interfaces.
    $endgroup$
    – Arthur Havlicek
    Sep 19 at 9:38











  • $begingroup$
    @ArthurHavlicek I guess after reading Gloweyes response, my question might not be as desired, but the response is a perfect kind of code review :)
    $endgroup$
    – evilive
    Sep 19 at 11:10










  • $begingroup$
    Just to be clear, I didn't mean that your question is offtopic there because you did post code to be reviewed and it's acceptable scopes of stack exchanges overlap a bit but if you did want a theoritical, elaborate point of view about structuring your classes API, with pro/cons of alternatives based on your requirements, then SE was more likely to give that. I actually would be surprised you don't already find good practices questions about classmethods there, if you are interested.
    $endgroup$
    – Arthur Havlicek
    Sep 19 at 11:38











  • $begingroup$
    Have you ever used the docopt library? You just write the help docstring and it automatically figures out everything from that.
    $endgroup$
    – Reinstate Monica
    Sep 19 at 17:49










  • $begingroup$
    @Justin No I didn'i know it yet, thank you. Allthough it reads nice I don't think that I'm gonna use it: The helptext and parameter description printed out with -h for the user mustn't necessarily be same information as the docstring for the developer.
    $endgroup$
    – evilive
    Sep 20 at 5:31













5













5









5





$begingroup$


I'm just trying to learn python: I tend to write my own customized wrappers arround "more complicated" python functionalities so that i can use them more easily, more consistent, easier to change in one place, etc.



I.e. if wrote my own class my_os with i.e. has the functions rm(path) or mv(from_path, to_path) so that on application- or script-level I don't need to know or care (or remember ;)) about the specialities of os , shutil, pathlib or whatever.



I did the same for argparse. Even though my own functions are quite sparse in this case, I still prefer a named my_add_required_arg over worrying about the action parameter of the native function in every script I write.



So much to the history of this class:



#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""Wrapper functionality for Pythons built-in argparse package."""

import argparse


class myArgparser(argparse.ArgumentParser):
"""Offers a wrapper for Pythons built-in argparse package.

It offers simple named functions to

* add a brief description
* set a version
* set optional named arguments
* set non optional named arguments
* set optional flags

and then to return the parsed arguments.
"""

def __init__(self, description=None):
"""Create the main argparser object.

Args:
description: The optional description.
"""
super(myArgparser, self).__init__(description=description)

# required arguments is just printed out on "-h"
self.myRequired = self.add_argument_group("required arguments")

def my_set_version(self, version):
"""Set the version information, printed out by --version.

Args:
version: version string
"""
self.add_argument("--version", action="version", version=version)

def my_add_flag(self, short, long, help):
"""Add a boolean flag.

If it is set, the flag argument is set to True, else to False.

Args:
short: the short name, i.e. '-d'
long: the long name, i.e. '--debug'
help: description printed out on '-h'
"""
self.add_argument(short, long, help=help, action="store_true")

def my_add_arg(self, short, long, help):
"""Add an optional named argument of string type.

If it isn't set on the commandline, it defaults to None.

Args:
short: the short name, i.e. '-ld'
long: the long name, i.e. '--logdir'
help: description printed out on '-h'
"""
self.add_argument(short, long, help=help, action="store")

def my_add_required_arg(self, short, long, help):
"""Add an required named argument of string Type.

Args:
short: the short name, i.e. '-ld'
long: the long name, i.e. '--logdir'
help: description printed out on '-h'
"""
self.myRequired.add_argument(
short, long, help=help, action="store", required=True
)

def my_get_args(self):
"""Return the arguments depending on the configured and set arguments.

Return:
Namespace-Object containing values for all added arguments
"""
return self.parse_args()


But using this class, I realized that I was using the same functions with oftenly the same parameters at the beginning of each of my scripts which needed arguments, so i added another function:



@classmethod
def get_args(
cls,
description=None,
version=None,
optional_args=[],
required_args=[],
flags=[],
):
"""Return the parsed arguments.

Each passed argument or flag tuple must contain the short name, the long name and the description
of the argument.

Args:
description: the optional description
version: the optional version string
optional_args: as list of tuples
required_args: as list of tuples
flags: as list of tuples
"""
argparser = cls(description)

if version is not None:
argparser.my_set_version(version)

for arg in optional_args:
argparser.my_add_arg(*arg)

for arg in required_args:
argparser.my_add_required_arg(*arg)

for flag in flags:
argparser.my_add_flag(*flag)

return argparser.my_get_args()



So now I can start a script with a clean argument definition, which I like:



# set global vars & args
version = 'mySpeedtest-20190918'
description = "Test the actual internet connection speed and log the result"
optional_args = [('-ld','--logdir', 'set destination directory for the logfile')]
flags = [('-d', '--debug', 'set loglevel to debug')]

args = my_util.my_argparse.myArgparser.get_args(description, version, optional_args, flags=flags)


Furthermore I don't have bloated init-function and still could use the single functions if I allready strongly had or wanted to.



Now the question: Does it make sense/is it good practice/is it "pythonic" to use a @classmethod as a pseudo-constructer like this?



(Kind of "Because you can do it, doesn't mean necessarily you should do it!"-Question ;))










share|improve this question











$endgroup$




I'm just trying to learn python: I tend to write my own customized wrappers arround "more complicated" python functionalities so that i can use them more easily, more consistent, easier to change in one place, etc.



I.e. if wrote my own class my_os with i.e. has the functions rm(path) or mv(from_path, to_path) so that on application- or script-level I don't need to know or care (or remember ;)) about the specialities of os , shutil, pathlib or whatever.



I did the same for argparse. Even though my own functions are quite sparse in this case, I still prefer a named my_add_required_arg over worrying about the action parameter of the native function in every script I write.



So much to the history of this class:



#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""Wrapper functionality for Pythons built-in argparse package."""

import argparse


class myArgparser(argparse.ArgumentParser):
"""Offers a wrapper for Pythons built-in argparse package.

It offers simple named functions to

* add a brief description
* set a version
* set optional named arguments
* set non optional named arguments
* set optional flags

and then to return the parsed arguments.
"""

def __init__(self, description=None):
"""Create the main argparser object.

Args:
description: The optional description.
"""
super(myArgparser, self).__init__(description=description)

# required arguments is just printed out on "-h"
self.myRequired = self.add_argument_group("required arguments")

def my_set_version(self, version):
"""Set the version information, printed out by --version.

Args:
version: version string
"""
self.add_argument("--version", action="version", version=version)

def my_add_flag(self, short, long, help):
"""Add a boolean flag.

If it is set, the flag argument is set to True, else to False.

Args:
short: the short name, i.e. '-d'
long: the long name, i.e. '--debug'
help: description printed out on '-h'
"""
self.add_argument(short, long, help=help, action="store_true")

def my_add_arg(self, short, long, help):
"""Add an optional named argument of string type.

If it isn't set on the commandline, it defaults to None.

Args:
short: the short name, i.e. '-ld'
long: the long name, i.e. '--logdir'
help: description printed out on '-h'
"""
self.add_argument(short, long, help=help, action="store")

def my_add_required_arg(self, short, long, help):
"""Add an required named argument of string Type.

Args:
short: the short name, i.e. '-ld'
long: the long name, i.e. '--logdir'
help: description printed out on '-h'
"""
self.myRequired.add_argument(
short, long, help=help, action="store", required=True
)

def my_get_args(self):
"""Return the arguments depending on the configured and set arguments.

Return:
Namespace-Object containing values for all added arguments
"""
return self.parse_args()


But using this class, I realized that I was using the same functions with oftenly the same parameters at the beginning of each of my scripts which needed arguments, so i added another function:



@classmethod
def get_args(
cls,
description=None,
version=None,
optional_args=[],
required_args=[],
flags=[],
):
"""Return the parsed arguments.

Each passed argument or flag tuple must contain the short name, the long name and the description
of the argument.

Args:
description: the optional description
version: the optional version string
optional_args: as list of tuples
required_args: as list of tuples
flags: as list of tuples
"""
argparser = cls(description)

if version is not None:
argparser.my_set_version(version)

for arg in optional_args:
argparser.my_add_arg(*arg)

for arg in required_args:
argparser.my_add_required_arg(*arg)

for flag in flags:
argparser.my_add_flag(*flag)

return argparser.my_get_args()



So now I can start a script with a clean argument definition, which I like:



# set global vars & args
version = 'mySpeedtest-20190918'
description = "Test the actual internet connection speed and log the result"
optional_args = [('-ld','--logdir', 'set destination directory for the logfile')]
flags = [('-d', '--debug', 'set loglevel to debug')]

args = my_util.my_argparse.myArgparser.get_args(description, version, optional_args, flags=flags)


Furthermore I don't have bloated init-function and still could use the single functions if I allready strongly had or wanted to.



Now the question: Does it make sense/is it good practice/is it "pythonic" to use a @classmethod as a pseudo-constructer like this?



(Kind of "Because you can do it, doesn't mean necessarily you should do it!"-Question ;))







python beginner python-3.x object-oriented wrapper






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 19 at 14:07









200_success

138k21 gold badges175 silver badges448 bronze badges




138k21 gold badges175 silver badges448 bronze badges










asked Sep 19 at 9:26









eviliveevilive

1532 bronze badges




1532 bronze badges










  • 1




    $begingroup$
    Your question "Is it good practice to use a @classmethod as a pseudo-constructer ? " is a question that I feel belongs more to Software Engineering. In general Code Review will give you more advices about how to structure implementation, while SE helps more structuring interfaces.
    $endgroup$
    – Arthur Havlicek
    Sep 19 at 9:38











  • $begingroup$
    @ArthurHavlicek I guess after reading Gloweyes response, my question might not be as desired, but the response is a perfect kind of code review :)
    $endgroup$
    – evilive
    Sep 19 at 11:10










  • $begingroup$
    Just to be clear, I didn't mean that your question is offtopic there because you did post code to be reviewed and it's acceptable scopes of stack exchanges overlap a bit but if you did want a theoritical, elaborate point of view about structuring your classes API, with pro/cons of alternatives based on your requirements, then SE was more likely to give that. I actually would be surprised you don't already find good practices questions about classmethods there, if you are interested.
    $endgroup$
    – Arthur Havlicek
    Sep 19 at 11:38











  • $begingroup$
    Have you ever used the docopt library? You just write the help docstring and it automatically figures out everything from that.
    $endgroup$
    – Reinstate Monica
    Sep 19 at 17:49










  • $begingroup$
    @Justin No I didn'i know it yet, thank you. Allthough it reads nice I don't think that I'm gonna use it: The helptext and parameter description printed out with -h for the user mustn't necessarily be same information as the docstring for the developer.
    $endgroup$
    – evilive
    Sep 20 at 5:31












  • 1




    $begingroup$
    Your question "Is it good practice to use a @classmethod as a pseudo-constructer ? " is a question that I feel belongs more to Software Engineering. In general Code Review will give you more advices about how to structure implementation, while SE helps more structuring interfaces.
    $endgroup$
    – Arthur Havlicek
    Sep 19 at 9:38











  • $begingroup$
    @ArthurHavlicek I guess after reading Gloweyes response, my question might not be as desired, but the response is a perfect kind of code review :)
    $endgroup$
    – evilive
    Sep 19 at 11:10










  • $begingroup$
    Just to be clear, I didn't mean that your question is offtopic there because you did post code to be reviewed and it's acceptable scopes of stack exchanges overlap a bit but if you did want a theoritical, elaborate point of view about structuring your classes API, with pro/cons of alternatives based on your requirements, then SE was more likely to give that. I actually would be surprised you don't already find good practices questions about classmethods there, if you are interested.
    $endgroup$
    – Arthur Havlicek
    Sep 19 at 11:38











  • $begingroup$
    Have you ever used the docopt library? You just write the help docstring and it automatically figures out everything from that.
    $endgroup$
    – Reinstate Monica
    Sep 19 at 17:49










  • $begingroup$
    @Justin No I didn'i know it yet, thank you. Allthough it reads nice I don't think that I'm gonna use it: The helptext and parameter description printed out with -h for the user mustn't necessarily be same information as the docstring for the developer.
    $endgroup$
    – evilive
    Sep 20 at 5:31







1




1




$begingroup$
Your question "Is it good practice to use a @classmethod as a pseudo-constructer ? " is a question that I feel belongs more to Software Engineering. In general Code Review will give you more advices about how to structure implementation, while SE helps more structuring interfaces.
$endgroup$
– Arthur Havlicek
Sep 19 at 9:38





$begingroup$
Your question "Is it good practice to use a @classmethod as a pseudo-constructer ? " is a question that I feel belongs more to Software Engineering. In general Code Review will give you more advices about how to structure implementation, while SE helps more structuring interfaces.
$endgroup$
– Arthur Havlicek
Sep 19 at 9:38













$begingroup$
@ArthurHavlicek I guess after reading Gloweyes response, my question might not be as desired, but the response is a perfect kind of code review :)
$endgroup$
– evilive
Sep 19 at 11:10




$begingroup$
@ArthurHavlicek I guess after reading Gloweyes response, my question might not be as desired, but the response is a perfect kind of code review :)
$endgroup$
– evilive
Sep 19 at 11:10












$begingroup$
Just to be clear, I didn't mean that your question is offtopic there because you did post code to be reviewed and it's acceptable scopes of stack exchanges overlap a bit but if you did want a theoritical, elaborate point of view about structuring your classes API, with pro/cons of alternatives based on your requirements, then SE was more likely to give that. I actually would be surprised you don't already find good practices questions about classmethods there, if you are interested.
$endgroup$
– Arthur Havlicek
Sep 19 at 11:38





$begingroup$
Just to be clear, I didn't mean that your question is offtopic there because you did post code to be reviewed and it's acceptable scopes of stack exchanges overlap a bit but if you did want a theoritical, elaborate point of view about structuring your classes API, with pro/cons of alternatives based on your requirements, then SE was more likely to give that. I actually would be surprised you don't already find good practices questions about classmethods there, if you are interested.
$endgroup$
– Arthur Havlicek
Sep 19 at 11:38













$begingroup$
Have you ever used the docopt library? You just write the help docstring and it automatically figures out everything from that.
$endgroup$
– Reinstate Monica
Sep 19 at 17:49




$begingroup$
Have you ever used the docopt library? You just write the help docstring and it automatically figures out everything from that.
$endgroup$
– Reinstate Monica
Sep 19 at 17:49












$begingroup$
@Justin No I didn'i know it yet, thank you. Allthough it reads nice I don't think that I'm gonna use it: The helptext and parameter description printed out with -h for the user mustn't necessarily be same information as the docstring for the developer.
$endgroup$
– evilive
Sep 20 at 5:31




$begingroup$
@Justin No I didn'i know it yet, thank you. Allthough it reads nice I don't think that I'm gonna use it: The helptext and parameter description printed out with -h for the user mustn't necessarily be same information as the docstring for the developer.
$endgroup$
– evilive
Sep 20 at 5:31










2 Answers
2






active

oldest

votes


















2

















$begingroup$

I disagree on you with the need for a wrapper around the stdlib argparse, but that's a matter of taste. And I don't see anything wrong with your implementation of the rather thin wrapper.



Classmethod as Constructor



This is NOT what you're doing. A constructor creates an instance in a way somehow different from the standard and returns it. An example:



class Thing:
def __init__(self, arg1, arg2)
self.var1 = arg1
self.var2 = arg2

@classmethod
def from_other_thing(cls, other_thing)
return cls(other_thing.somevar, other_thing.othervar)


Your usecase



What you're doing is creating a class method that creates an instance to do work, then returning the results of that work. I'd like to emphasize that this is a perfectly valid usecase. However, your classmethod is also just about doing what you made the rest of your wrapper class for. You can easily cut down on the amount of new methods like this:



class myArgparser(argparse.ArgumentParser):
""" Your perfectly fine docstring """

@classmethod
def get_args(
cls,
description=None,
version=None,
optional_args=None,
required_args=None,
flags=None,
):
"""Your other perfectly fine docstring"""
optional_args = optional_args if optional_args is not None else []
required_args = required_args if required_args is not None else []
flags = flags if flags is not None else []

argparser = cls(description)
if version is not None:
argparser.add_argument("--version", action="version", version=version)
for arg in optional_args:
argparser.add_argument(*arg, action="store")
for arg in required_args:
argparser.add_argument(*arg, action="store", required=True)
for flag in flags:
argparser.add_argument(*flag, action="store_true")
return argparser.parse_args()



I reduced the whitespace a bit - to many newlines make reading harder. I've also changed the get_args argument to do the work you had spread over all the other 1-line functions - if a function is 1 line and used in just 1 place, then you don't really need a function for it. That's premature optimization.



Mutable Default Arguments



Don't use mutable defaults. Instead, use None and create empty lists, or change the default to an empty tuple, which is safe. I've gone for the first option, but as far as I can tell the empty tuple version would be perfectly fine as well.






share|improve this answer










$endgroup$













  • $begingroup$
    Thank you very much. Reading your version I had to ask myself, why I didn't do it like that "out of the box" ;) Also the reminder not to use mutable defaults is a nice one. I even read that once before, but just didn't think of it after my first test ended with the result "cannot iterate over NoneType" :D
    $endgroup$
    – evilive
    Sep 19 at 11:29










  • $begingroup$
    "Why didn't I do it like that" is a rather common thought when developing. I've had it about my own code more than I can remember. Glad to help.
    $endgroup$
    – Gloweye
    Sep 19 at 11:30










  • $begingroup$
    If you are reducing the whole class to a single classmethod, you might as well simplify it to a simple function.
    $endgroup$
    – 409_Conflict
    Sep 19 at 11:39










  • $begingroup$
    This class still extends the ArgumentParser class. But yes, it could also be a stand-alone function that receives the class as argument. I think both are valid choices.
    $endgroup$
    – Gloweye
    Sep 19 at 11:50


















3

















$begingroup$

Using @classmethod for constructors is perfectly fine. It is basically the only reason you ever need to use them.



Classes create a namespace (and Namespaces are one honking great idea -- let's do more of those!). You don't need to preface all your methods and classes with my_. Your code becomes much more readable if you just name them according to what they do:



class ArgParser(argparse.ArgumentParser):
...
def set_version(self, version):
...
def add_flag(self, short, long, help):
...
...


Python also has an official style-guide, PEP8, which programmers are recommended to follow. It recommends using lower_case for methods and variables.



In general I am not a huge fan of this class. It makes building an argument parser slightly easier (nice!) at the cost of hiding away all advanced functionality (not so nice). I would try finding an interface that at least allows you to add additional options (like specifying types, defaults, nargs, etc). Maybe each argument is also allowed to be a tuple of a tuple and a dictionary (for positional and keyword arguments)?






share|improve this answer












$endgroup$













  • $begingroup$
    Thank you for your answer. Concerning namespaces: yes you're right and in general i don't preface all my methods. But in this special case, as my Class inherits from the general ArgumentParser I just wanted to make sure not to "hide" something of it (without the need of checking if it exists or not ;)) At the moment the functionality of Argparse is totally fine for me.
    $endgroup$
    – evilive
    Sep 19 at 11:20












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);














draft saved

draft discarded
















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f229300%2fwrapper-for-pythons-argparse-class%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown


























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2

















$begingroup$

I disagree on you with the need for a wrapper around the stdlib argparse, but that's a matter of taste. And I don't see anything wrong with your implementation of the rather thin wrapper.



Classmethod as Constructor



This is NOT what you're doing. A constructor creates an instance in a way somehow different from the standard and returns it. An example:



class Thing:
def __init__(self, arg1, arg2)
self.var1 = arg1
self.var2 = arg2

@classmethod
def from_other_thing(cls, other_thing)
return cls(other_thing.somevar, other_thing.othervar)


Your usecase



What you're doing is creating a class method that creates an instance to do work, then returning the results of that work. I'd like to emphasize that this is a perfectly valid usecase. However, your classmethod is also just about doing what you made the rest of your wrapper class for. You can easily cut down on the amount of new methods like this:



class myArgparser(argparse.ArgumentParser):
""" Your perfectly fine docstring """

@classmethod
def get_args(
cls,
description=None,
version=None,
optional_args=None,
required_args=None,
flags=None,
):
"""Your other perfectly fine docstring"""
optional_args = optional_args if optional_args is not None else []
required_args = required_args if required_args is not None else []
flags = flags if flags is not None else []

argparser = cls(description)
if version is not None:
argparser.add_argument("--version", action="version", version=version)
for arg in optional_args:
argparser.add_argument(*arg, action="store")
for arg in required_args:
argparser.add_argument(*arg, action="store", required=True)
for flag in flags:
argparser.add_argument(*flag, action="store_true")
return argparser.parse_args()



I reduced the whitespace a bit - to many newlines make reading harder. I've also changed the get_args argument to do the work you had spread over all the other 1-line functions - if a function is 1 line and used in just 1 place, then you don't really need a function for it. That's premature optimization.



Mutable Default Arguments



Don't use mutable defaults. Instead, use None and create empty lists, or change the default to an empty tuple, which is safe. I've gone for the first option, but as far as I can tell the empty tuple version would be perfectly fine as well.






share|improve this answer










$endgroup$













  • $begingroup$
    Thank you very much. Reading your version I had to ask myself, why I didn't do it like that "out of the box" ;) Also the reminder not to use mutable defaults is a nice one. I even read that once before, but just didn't think of it after my first test ended with the result "cannot iterate over NoneType" :D
    $endgroup$
    – evilive
    Sep 19 at 11:29










  • $begingroup$
    "Why didn't I do it like that" is a rather common thought when developing. I've had it about my own code more than I can remember. Glad to help.
    $endgroup$
    – Gloweye
    Sep 19 at 11:30










  • $begingroup$
    If you are reducing the whole class to a single classmethod, you might as well simplify it to a simple function.
    $endgroup$
    – 409_Conflict
    Sep 19 at 11:39










  • $begingroup$
    This class still extends the ArgumentParser class. But yes, it could also be a stand-alone function that receives the class as argument. I think both are valid choices.
    $endgroup$
    – Gloweye
    Sep 19 at 11:50















2

















$begingroup$

I disagree on you with the need for a wrapper around the stdlib argparse, but that's a matter of taste. And I don't see anything wrong with your implementation of the rather thin wrapper.



Classmethod as Constructor



This is NOT what you're doing. A constructor creates an instance in a way somehow different from the standard and returns it. An example:



class Thing:
def __init__(self, arg1, arg2)
self.var1 = arg1
self.var2 = arg2

@classmethod
def from_other_thing(cls, other_thing)
return cls(other_thing.somevar, other_thing.othervar)


Your usecase



What you're doing is creating a class method that creates an instance to do work, then returning the results of that work. I'd like to emphasize that this is a perfectly valid usecase. However, your classmethod is also just about doing what you made the rest of your wrapper class for. You can easily cut down on the amount of new methods like this:



class myArgparser(argparse.ArgumentParser):
""" Your perfectly fine docstring """

@classmethod
def get_args(
cls,
description=None,
version=None,
optional_args=None,
required_args=None,
flags=None,
):
"""Your other perfectly fine docstring"""
optional_args = optional_args if optional_args is not None else []
required_args = required_args if required_args is not None else []
flags = flags if flags is not None else []

argparser = cls(description)
if version is not None:
argparser.add_argument("--version", action="version", version=version)
for arg in optional_args:
argparser.add_argument(*arg, action="store")
for arg in required_args:
argparser.add_argument(*arg, action="store", required=True)
for flag in flags:
argparser.add_argument(*flag, action="store_true")
return argparser.parse_args()



I reduced the whitespace a bit - to many newlines make reading harder. I've also changed the get_args argument to do the work you had spread over all the other 1-line functions - if a function is 1 line and used in just 1 place, then you don't really need a function for it. That's premature optimization.



Mutable Default Arguments



Don't use mutable defaults. Instead, use None and create empty lists, or change the default to an empty tuple, which is safe. I've gone for the first option, but as far as I can tell the empty tuple version would be perfectly fine as well.






share|improve this answer










$endgroup$













  • $begingroup$
    Thank you very much. Reading your version I had to ask myself, why I didn't do it like that "out of the box" ;) Also the reminder not to use mutable defaults is a nice one. I even read that once before, but just didn't think of it after my first test ended with the result "cannot iterate over NoneType" :D
    $endgroup$
    – evilive
    Sep 19 at 11:29










  • $begingroup$
    "Why didn't I do it like that" is a rather common thought when developing. I've had it about my own code more than I can remember. Glad to help.
    $endgroup$
    – Gloweye
    Sep 19 at 11:30










  • $begingroup$
    If you are reducing the whole class to a single classmethod, you might as well simplify it to a simple function.
    $endgroup$
    – 409_Conflict
    Sep 19 at 11:39










  • $begingroup$
    This class still extends the ArgumentParser class. But yes, it could also be a stand-alone function that receives the class as argument. I think both are valid choices.
    $endgroup$
    – Gloweye
    Sep 19 at 11:50













2















2











2







$begingroup$

I disagree on you with the need for a wrapper around the stdlib argparse, but that's a matter of taste. And I don't see anything wrong with your implementation of the rather thin wrapper.



Classmethod as Constructor



This is NOT what you're doing. A constructor creates an instance in a way somehow different from the standard and returns it. An example:



class Thing:
def __init__(self, arg1, arg2)
self.var1 = arg1
self.var2 = arg2

@classmethod
def from_other_thing(cls, other_thing)
return cls(other_thing.somevar, other_thing.othervar)


Your usecase



What you're doing is creating a class method that creates an instance to do work, then returning the results of that work. I'd like to emphasize that this is a perfectly valid usecase. However, your classmethod is also just about doing what you made the rest of your wrapper class for. You can easily cut down on the amount of new methods like this:



class myArgparser(argparse.ArgumentParser):
""" Your perfectly fine docstring """

@classmethod
def get_args(
cls,
description=None,
version=None,
optional_args=None,
required_args=None,
flags=None,
):
"""Your other perfectly fine docstring"""
optional_args = optional_args if optional_args is not None else []
required_args = required_args if required_args is not None else []
flags = flags if flags is not None else []

argparser = cls(description)
if version is not None:
argparser.add_argument("--version", action="version", version=version)
for arg in optional_args:
argparser.add_argument(*arg, action="store")
for arg in required_args:
argparser.add_argument(*arg, action="store", required=True)
for flag in flags:
argparser.add_argument(*flag, action="store_true")
return argparser.parse_args()



I reduced the whitespace a bit - to many newlines make reading harder. I've also changed the get_args argument to do the work you had spread over all the other 1-line functions - if a function is 1 line and used in just 1 place, then you don't really need a function for it. That's premature optimization.



Mutable Default Arguments



Don't use mutable defaults. Instead, use None and create empty lists, or change the default to an empty tuple, which is safe. I've gone for the first option, but as far as I can tell the empty tuple version would be perfectly fine as well.






share|improve this answer










$endgroup$



I disagree on you with the need for a wrapper around the stdlib argparse, but that's a matter of taste. And I don't see anything wrong with your implementation of the rather thin wrapper.



Classmethod as Constructor



This is NOT what you're doing. A constructor creates an instance in a way somehow different from the standard and returns it. An example:



class Thing:
def __init__(self, arg1, arg2)
self.var1 = arg1
self.var2 = arg2

@classmethod
def from_other_thing(cls, other_thing)
return cls(other_thing.somevar, other_thing.othervar)


Your usecase



What you're doing is creating a class method that creates an instance to do work, then returning the results of that work. I'd like to emphasize that this is a perfectly valid usecase. However, your classmethod is also just about doing what you made the rest of your wrapper class for. You can easily cut down on the amount of new methods like this:



class myArgparser(argparse.ArgumentParser):
""" Your perfectly fine docstring """

@classmethod
def get_args(
cls,
description=None,
version=None,
optional_args=None,
required_args=None,
flags=None,
):
"""Your other perfectly fine docstring"""
optional_args = optional_args if optional_args is not None else []
required_args = required_args if required_args is not None else []
flags = flags if flags is not None else []

argparser = cls(description)
if version is not None:
argparser.add_argument("--version", action="version", version=version)
for arg in optional_args:
argparser.add_argument(*arg, action="store")
for arg in required_args:
argparser.add_argument(*arg, action="store", required=True)
for flag in flags:
argparser.add_argument(*flag, action="store_true")
return argparser.parse_args()



I reduced the whitespace a bit - to many newlines make reading harder. I've also changed the get_args argument to do the work you had spread over all the other 1-line functions - if a function is 1 line and used in just 1 place, then you don't really need a function for it. That's premature optimization.



Mutable Default Arguments



Don't use mutable defaults. Instead, use None and create empty lists, or change the default to an empty tuple, which is safe. I've gone for the first option, but as far as I can tell the empty tuple version would be perfectly fine as well.







share|improve this answer













share|improve this answer




share|improve this answer










answered Sep 19 at 10:36









GloweyeGloweye

1,7055 silver badges19 bronze badges




1,7055 silver badges19 bronze badges














  • $begingroup$
    Thank you very much. Reading your version I had to ask myself, why I didn't do it like that "out of the box" ;) Also the reminder not to use mutable defaults is a nice one. I even read that once before, but just didn't think of it after my first test ended with the result "cannot iterate over NoneType" :D
    $endgroup$
    – evilive
    Sep 19 at 11:29










  • $begingroup$
    "Why didn't I do it like that" is a rather common thought when developing. I've had it about my own code more than I can remember. Glad to help.
    $endgroup$
    – Gloweye
    Sep 19 at 11:30










  • $begingroup$
    If you are reducing the whole class to a single classmethod, you might as well simplify it to a simple function.
    $endgroup$
    – 409_Conflict
    Sep 19 at 11:39










  • $begingroup$
    This class still extends the ArgumentParser class. But yes, it could also be a stand-alone function that receives the class as argument. I think both are valid choices.
    $endgroup$
    – Gloweye
    Sep 19 at 11:50
















  • $begingroup$
    Thank you very much. Reading your version I had to ask myself, why I didn't do it like that "out of the box" ;) Also the reminder not to use mutable defaults is a nice one. I even read that once before, but just didn't think of it after my first test ended with the result "cannot iterate over NoneType" :D
    $endgroup$
    – evilive
    Sep 19 at 11:29










  • $begingroup$
    "Why didn't I do it like that" is a rather common thought when developing. I've had it about my own code more than I can remember. Glad to help.
    $endgroup$
    – Gloweye
    Sep 19 at 11:30










  • $begingroup$
    If you are reducing the whole class to a single classmethod, you might as well simplify it to a simple function.
    $endgroup$
    – 409_Conflict
    Sep 19 at 11:39










  • $begingroup$
    This class still extends the ArgumentParser class. But yes, it could also be a stand-alone function that receives the class as argument. I think both are valid choices.
    $endgroup$
    – Gloweye
    Sep 19 at 11:50















$begingroup$
Thank you very much. Reading your version I had to ask myself, why I didn't do it like that "out of the box" ;) Also the reminder not to use mutable defaults is a nice one. I even read that once before, but just didn't think of it after my first test ended with the result "cannot iterate over NoneType" :D
$endgroup$
– evilive
Sep 19 at 11:29




$begingroup$
Thank you very much. Reading your version I had to ask myself, why I didn't do it like that "out of the box" ;) Also the reminder not to use mutable defaults is a nice one. I even read that once before, but just didn't think of it after my first test ended with the result "cannot iterate over NoneType" :D
$endgroup$
– evilive
Sep 19 at 11:29












$begingroup$
"Why didn't I do it like that" is a rather common thought when developing. I've had it about my own code more than I can remember. Glad to help.
$endgroup$
– Gloweye
Sep 19 at 11:30




$begingroup$
"Why didn't I do it like that" is a rather common thought when developing. I've had it about my own code more than I can remember. Glad to help.
$endgroup$
– Gloweye
Sep 19 at 11:30












$begingroup$
If you are reducing the whole class to a single classmethod, you might as well simplify it to a simple function.
$endgroup$
– 409_Conflict
Sep 19 at 11:39




$begingroup$
If you are reducing the whole class to a single classmethod, you might as well simplify it to a simple function.
$endgroup$
– 409_Conflict
Sep 19 at 11:39












$begingroup$
This class still extends the ArgumentParser class. But yes, it could also be a stand-alone function that receives the class as argument. I think both are valid choices.
$endgroup$
– Gloweye
Sep 19 at 11:50




$begingroup$
This class still extends the ArgumentParser class. But yes, it could also be a stand-alone function that receives the class as argument. I think both are valid choices.
$endgroup$
– Gloweye
Sep 19 at 11:50













3

















$begingroup$

Using @classmethod for constructors is perfectly fine. It is basically the only reason you ever need to use them.



Classes create a namespace (and Namespaces are one honking great idea -- let's do more of those!). You don't need to preface all your methods and classes with my_. Your code becomes much more readable if you just name them according to what they do:



class ArgParser(argparse.ArgumentParser):
...
def set_version(self, version):
...
def add_flag(self, short, long, help):
...
...


Python also has an official style-guide, PEP8, which programmers are recommended to follow. It recommends using lower_case for methods and variables.



In general I am not a huge fan of this class. It makes building an argument parser slightly easier (nice!) at the cost of hiding away all advanced functionality (not so nice). I would try finding an interface that at least allows you to add additional options (like specifying types, defaults, nargs, etc). Maybe each argument is also allowed to be a tuple of a tuple and a dictionary (for positional and keyword arguments)?






share|improve this answer












$endgroup$













  • $begingroup$
    Thank you for your answer. Concerning namespaces: yes you're right and in general i don't preface all my methods. But in this special case, as my Class inherits from the general ArgumentParser I just wanted to make sure not to "hide" something of it (without the need of checking if it exists or not ;)) At the moment the functionality of Argparse is totally fine for me.
    $endgroup$
    – evilive
    Sep 19 at 11:20















3

















$begingroup$

Using @classmethod for constructors is perfectly fine. It is basically the only reason you ever need to use them.



Classes create a namespace (and Namespaces are one honking great idea -- let's do more of those!). You don't need to preface all your methods and classes with my_. Your code becomes much more readable if you just name them according to what they do:



class ArgParser(argparse.ArgumentParser):
...
def set_version(self, version):
...
def add_flag(self, short, long, help):
...
...


Python also has an official style-guide, PEP8, which programmers are recommended to follow. It recommends using lower_case for methods and variables.



In general I am not a huge fan of this class. It makes building an argument parser slightly easier (nice!) at the cost of hiding away all advanced functionality (not so nice). I would try finding an interface that at least allows you to add additional options (like specifying types, defaults, nargs, etc). Maybe each argument is also allowed to be a tuple of a tuple and a dictionary (for positional and keyword arguments)?






share|improve this answer












$endgroup$













  • $begingroup$
    Thank you for your answer. Concerning namespaces: yes you're right and in general i don't preface all my methods. But in this special case, as my Class inherits from the general ArgumentParser I just wanted to make sure not to "hide" something of it (without the need of checking if it exists or not ;)) At the moment the functionality of Argparse is totally fine for me.
    $endgroup$
    – evilive
    Sep 19 at 11:20













3















3











3







$begingroup$

Using @classmethod for constructors is perfectly fine. It is basically the only reason you ever need to use them.



Classes create a namespace (and Namespaces are one honking great idea -- let's do more of those!). You don't need to preface all your methods and classes with my_. Your code becomes much more readable if you just name them according to what they do:



class ArgParser(argparse.ArgumentParser):
...
def set_version(self, version):
...
def add_flag(self, short, long, help):
...
...


Python also has an official style-guide, PEP8, which programmers are recommended to follow. It recommends using lower_case for methods and variables.



In general I am not a huge fan of this class. It makes building an argument parser slightly easier (nice!) at the cost of hiding away all advanced functionality (not so nice). I would try finding an interface that at least allows you to add additional options (like specifying types, defaults, nargs, etc). Maybe each argument is also allowed to be a tuple of a tuple and a dictionary (for positional and keyword arguments)?






share|improve this answer












$endgroup$



Using @classmethod for constructors is perfectly fine. It is basically the only reason you ever need to use them.



Classes create a namespace (and Namespaces are one honking great idea -- let's do more of those!). You don't need to preface all your methods and classes with my_. Your code becomes much more readable if you just name them according to what they do:



class ArgParser(argparse.ArgumentParser):
...
def set_version(self, version):
...
def add_flag(self, short, long, help):
...
...


Python also has an official style-guide, PEP8, which programmers are recommended to follow. It recommends using lower_case for methods and variables.



In general I am not a huge fan of this class. It makes building an argument parser slightly easier (nice!) at the cost of hiding away all advanced functionality (not so nice). I would try finding an interface that at least allows you to add additional options (like specifying types, defaults, nargs, etc). Maybe each argument is also allowed to be a tuple of a tuple and a dictionary (for positional and keyword arguments)?







share|improve this answer















share|improve this answer




share|improve this answer








edited Sep 19 at 10:36

























answered Sep 19 at 10:30









GraipherGraipher

32.9k6 gold badges51 silver badges110 bronze badges




32.9k6 gold badges51 silver badges110 bronze badges














  • $begingroup$
    Thank you for your answer. Concerning namespaces: yes you're right and in general i don't preface all my methods. But in this special case, as my Class inherits from the general ArgumentParser I just wanted to make sure not to "hide" something of it (without the need of checking if it exists or not ;)) At the moment the functionality of Argparse is totally fine for me.
    $endgroup$
    – evilive
    Sep 19 at 11:20
















  • $begingroup$
    Thank you for your answer. Concerning namespaces: yes you're right and in general i don't preface all my methods. But in this special case, as my Class inherits from the general ArgumentParser I just wanted to make sure not to "hide" something of it (without the need of checking if it exists or not ;)) At the moment the functionality of Argparse is totally fine for me.
    $endgroup$
    – evilive
    Sep 19 at 11:20















$begingroup$
Thank you for your answer. Concerning namespaces: yes you're right and in general i don't preface all my methods. But in this special case, as my Class inherits from the general ArgumentParser I just wanted to make sure not to "hide" something of it (without the need of checking if it exists or not ;)) At the moment the functionality of Argparse is totally fine for me.
$endgroup$
– evilive
Sep 19 at 11:20




$begingroup$
Thank you for your answer. Concerning namespaces: yes you're right and in general i don't preface all my methods. But in this special case, as my Class inherits from the general ArgumentParser I just wanted to make sure not to "hide" something of it (without the need of checking if it exists or not ;)) At the moment the functionality of Argparse is totally fine for me.
$endgroup$
– evilive
Sep 19 at 11:20


















draft saved

draft discarded















































Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f229300%2fwrapper-for-pythons-argparse-class%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown









Popular posts from this blog

Tamil (spriik) Luke uk diar | Nawigatjuun

Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

Training a classifier when some of the features are unknownWhy does Gradient Boosting regression predict negative values when there are no negative y-values in my training set?How to improve an existing (trained) classifier?What is effect when I set up some self defined predisctor variables?Why Matlab neural network classification returns decimal values on prediction dataset?Fitting and transforming text data in training, testing, and validation setsHow to quantify the performance of the classifier (multi-class SVM) using the test data?How do I control for some patients providing multiple samples in my training data?Training and Test setTraining a convolutional neural network for image denoising in MatlabShouldn't an autoencoder with #(neurons in hidden layer) = #(neurons in input layer) be “perfect”?