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

[PEP] Standardizing Pyomo Imports in examples as "import pyomo.environ as pyo" #1026

Open
1 of 3 tasks
whart222 opened this issue May 22, 2019 · 8 comments
Open
1 of 3 tasks
Labels

Comments

@whart222
Copy link
Member

whart222 commented May 22, 2019

(Edited by @carldlaird)
This PEP advocates use of the "pyo" alias throughout the documentation, pyomo examples, and tutorials.

Motivation

Users find it confusing that we have not "standardized" on a particular approach for imports (some use from pyomo.environ import *, some use import pyomo.environ as pe, and some use import pyomo.environ as pyo). We also agree that it is generally bad practice to import *, and would prefer to use advocate the use of a named alias when importing pyomo.environ.

Implementation

Standardize on "import pyomo.environ as pyo".

This will need to be done for:

  • examples in read-the-docs
  • examples in the pyomo repository (examples directory)
  • exercises and slides for Pyomo workshops

@carldlaird has agreed to implement this PEP.

Original Introduction from @whart222 included below:

Currently, three conventions are used in the Pyomo online documentation for importing Pyomo:

  • Symbols are imported from pyomo.environ
    """
    from pyomo.environ import *
    """
  • The pyomo.environ module is imported to an alias
    """
    import pyomo.environ as pyo
    """
  • The environment is imported, and symbols are referenced directly from the pyomo library
    """
    import pyomo.environ
    from pyomo.opt import SolverFactory
    """

A brand new user recently noted how confusing this made the online documentation. The fact that 'pyo' was used was particularly confusing because that alias is not included in most examples.

We need to standardize documentation to avoid such confusion, and provide more complete examples if we use an alias like 'pyo'. I think this PEP should include a specific recommendation to either use 'pyo' or not. Let's include comments for/against.

@blnicho
Copy link
Member

blnicho commented May 22, 2019

We should also propagate whatever import convention we choose to the workshop slides, examples, and exercises.

I personally use from pyomo.environ import * however my vote for standardization would be for import pyomo.environ as pyo (or I think some folks use import pyomo.environ as pe). I think importing to an alias is more Pythonic and it makes the code very explicit as to which classes/functions are coming from Pyomo.

@blnicho blnicho added the PEP label May 22, 2019
@jwatsonnm
Copy link
Contributor

I concur with @blnicho - and prefer to adopt "import pyomo.environ as pro" following our colleagues at NPS and elsewhere.

@carldlaird
Copy link
Member

Assuming you meant "pyo" instead of "pro", I agree as well.

@jwatsonnm
Copy link
Contributor

Whoops - yes. Stupid autocorrect. Or neurons.

@carldlaird
Copy link
Member

Bill above asked us to list comments against the use of an alias. The only one I can think about is that it adds some additional "pyo." to our code - that might not look as nice in examples. (maybe). This could be overcome with explicit imports (e.g., from pyomo.environ import ConcreteModel, Constraint, Objective, ...). But that list would get extensive, and I don't favor that approach over the pyo approach.

@jwatsonnm
Copy link
Contributor

We could ask everyone if they like "std::endl" or "endl" better - I bet the survey would predict who does and doesn't like aliases almost perfectly. For the record, I personally believe that "using namespace std" is evil. Sure, the alias makes short examples slightly longer. But the clarity has to outweigh the conciseness.

@DLWoodruff
Copy link
Contributor

I agree with standardizing to pyo; however, an interesting debate is based on the following: When Bill says "and provide more complete examples if we use an alias like 'pyo'" he seems to imply that every code snippet that uses pyo would , at a minimum, need to include
import pyomo.environ as pyo

There are many python packages that do that sort of thing, but our situation is a little different. Many of our code snippets rely on lots of things that are not rendered to the screen. These snippets are not intended to serve as a tutorial, but rather to let a user remember, or learn, a particular syntax.

@carldlaird carldlaird changed the title [PEP] Standardizing Pyomo Imports in Pyomo Documentation [PEP] Standardizing Pyomo Imports in examples as "import pyomo.environ as pro" Jun 4, 2019
@carldlaird carldlaird changed the title [PEP] Standardizing Pyomo Imports in examples as "import pyomo.environ as pro" [PEP] Standardizing Pyomo Imports in examples as "import pyomo.environ as pyo" Jun 4, 2019
@carldlaird carldlaird changed the title [PEP] Standardizing Pyomo Imports in examples as "import pyomo.environ as pyo" [PEP] Standardizing Pyomo Imports in examples as "import pyomo.environ as pyo" Jun 4, 2019
@whart222
Copy link
Member Author

whart222 commented Jun 4, 2019

@DLWoodruff FWIW, I'm OK not adding the import alias in every example, but per a user suggestion we should include a simple example at the beginning of our documentation to establish this convention.

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

No branches or pull requests

5 participants