Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extracting values from Python sourcefile variables #13

Open
Paebbels opened this issue Jan 12, 2020 · 1 comment
Open

Extracting values from Python sourcefile variables #13

Paebbels opened this issue Jan 12, 2020 · 1 comment

Comments

@Paebbels
Copy link
Contributor

I like that you extract information like author, version, etc. from a single source file.

Lately I came across this approach used in sphinxcontrib.autoprogram:

with open(os.path.join('..', 'setup.py')) as _f:
    _setup_ast = ast.parse(_f.read(), _f.name)
del _f
# The full version, including alpha/beta/rc tags.
release = next(
    node.value.s
    for node in ast.walk(_setup_ast)
    if (isinstance(node, ast.Assign) and len(node.targets) == 1 and
        isinstance(node.targets[0], ast.Name) and
        node.targets[0].id == 'version' and isinstance(node.value, ast.Str))
)

They use the ast (abstract syntax tree) package from Python to scan for a variable called version. I think this is a more elegant solution and more robust.

@Paebbels
Copy link
Contributor Author

This code snippet could be used e.g. in #18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant