Where do I find pyomo classes, methods and properties? - pyomo

I need a documentation about classes, methods and properties of Pyomo. Where do I find that. I have been looking for this dicumentation for some days.
Silvana Nobre

You can use
dir([object])ΒΆ
Without arguments, return the list of names in the current local scope. With an argument, attempt to return a list of valid attributes for that object.
m = AbstractModel()
print (dir(m))
will print all properties of the AbstractModel object

Related

Assign List<B> object to List<A> object

I have a problem that I want to know if there is a magic trick or pattern to solve this in the other way.
I have list of B class objects, but I need to return list of A class objects. 'A' and 'B' classes have exactly the same fields (I need to return List, but I have List because it was mapped from database by hibernate) and I need to return it inside List of A objects because I can not import B class to other project (where class A is) beacuse of maven cyclic reference.
I just rewrote all fields from every object in for loop.
Is there any other way to solve this without doing it inside the loop?
Thanks in advance for discussion :)
Cheers! :)
Did you try returning Object type list? It could then be type-casted to B type .
In a perfect world you would extend both classes from one base class to make them copatible, but i see that this is not possible here.
in my opinion your approch is already the savest way to solve the problem.
another way would be to solve it over reflection. It's not recomendable - just pointing this out.
In this solution, you iterate over all declared fields of the given object, read out the value and set the field with the same name in your object to this value.
like i said, it's not recomendable because it relies on field names which may change.

How do intellisense and autocompletions work?

I've been wondering for a while: how do autocompletions work?
Example:
In PhpStorm whenever I use a class and type -> it shows me all properties and methods of that class. It also auto completes namespaces and even functions inside libraries such as jQuery.
Does it run some sort of regex on the files, or does it parse them somehow?
PhpStorm developer here. I'd like to go through some basics in case it may be helpful for those who want to implement their own plugin.
First of all, a code has to be broken into tokens using a lexer. Then AST (an abstract syntax tree) and PSI (a program structure interface) are built using a parser. PhpStorm has its own implementations of the lexer and the parser. This is how a PSI tree looks like for a simple class.
When you type in an editor or explicitly invoke a completion action (Ctrl+Space) a number of completion contributors are invoked. They're intended to return a list of suggestions based on a cursor's position.
Let's consider a case when completion is invoked inside a field reference.
PhpStorm knows that at the current position all class members can be suggested. It starts obtaining a class reference (the $class variable in our case) and determining its type. If a variable resolves to a class its type is a class' FQN (fully qualified name).
To obtain methods and fields of a class its PSI element is needed. A special index is used to map an FQN to an appropriate PhpClass tree element. Indices are initially built when a project is opened for the first time and updated for each modified file.
PhpStorm collects all members from the PSI element (including parent's ones), then from its traits. They're filtered depending on a current context (e.g. access scope) and an already typed name's part (f).
Suggestions are shown in a list which is sorted by how good element's name matches, its type, position and so on. The list rearranges when you type.
When you press Enter to insert an element PhpStorm invokes one more handler. It knows how to properly insert the element into a code. For instance, it can add parentheses for a method or import a class reference. In our case, it's enough to put brackets and place a cursor just after them because the method has no parameters.
That's basically it. It worth mention that the IntelliJ IDEA platform allows a plugin to provide an implementation for each step described above. Thus completion can be improved or extended for some particular framework or language.

Scala empty a list

I have a member variable in a class:
val options = mutable.LinkedList[SelectOption]()
I latter then populate this list from the database.
At some point I want to refresh the list. How do I empty it?
In java:
options.clear();
Is there an equivalent in Scala?
Do not use LinkedList. That is a low level collection which provides a data structure that can be manipulated at user's will... and responsibility.
Instead, use one of the Buffer classes, which have the clear method. This method, by the way, is inherited from the Clearable trait, so you can just look at classes that extend Clearable.

Lua: how to verify that a table contains a specific function

I'm developing a module that returns a table full of functions based on the arguments that are passed in. Specifically, the module returns a set of data transformation rules (functions) that need to be applied to a data set depending on which customer is sending it.
I decided to decouple my rule library (biz logic) from the code that decides which of the rules should be applied (config logic).
Here's the unit test I'm writing to verify that the ruleBuilder is adding the correct rule (function) based on one of my scenarios:
ruleBuilder = require("ruleBuilder")
ruleLibrary = require("ruleLibrary")
local rules = ruleBuilder.assembleRules("Customer1231")
assert(rules[1] == ruleLibrary.missingSSNRule)
Is this the correct way to do that verification? Will this work even if the ruleLibrary.missingSSNRule function has references to several other functions via a closure or parameter?
To verify that a table contains a particular function you may use the fact that keys in Lua tables can be anything (including functions). In your assembleRules code you can write something like this:
function assembleRules(...)
...
return {
[someOtherCoolModule.coolFunction] = someOtherCoolModule.coolFunction,
[yetAnotherModule.anotherFunction] = yetAnotherModule.anotherFunction,
}
end
Then later you can simply check if the key exists:
local rules = ruleBuilder.assembleRules("somedata")
assert(rules[someOtherCoolModule.coolFunction])
On the assumption that the return value of ruleBuilder.assembleRules is supposed to somehow know to put someOtherCoolModule.coolFunction in the 0-th index (note: Lua uses 1-based indices. Don't use 0 as an index) of its return value, then yes.
Will this work even if someOtherCoolModule.coolFunction is a closure?
All functions in Lua are closures. However, I'm going to assume that you mean that ruleBuilder.assembleRules is going to take someOtherCoolModule.coolFunction and build a new function around it.
A function is equal to itself. But it is only equal to itself. Just like two tables are only equal if they are the same table object, two functions are only equal if they are the same function. Functions are not equal to a different instantiation of the same function, nor is it equal to any other function. Here are examples of this.

What is the Python counterpart to an Ada record / C++ struct type?

Suppose I am recording data and want to associate some number of data elements, such that each recorded set always has a fixed composition, i.e. no missing fields.
Most of my experience as a programmer is with Ada or C/C++ variants. In Ada, I would use a record type and aggregate assignment, so that when the record type was updated with new fields, anyone using the record would be notified by the compiler. In C++, chances are I would use a storage class and constructor to do something similar.
What is the appropriate way to handle a similar situation in Python? Is this a case where classes are the proper answer, or is there a lighter weight analog to the Ada record?
An additional thought, both Ada records and C++ constructors allow for default initialization values. Is there a Python solution to the above question which provides that capability as well?
A namedtuple (from the collections library) may suit your purposes. It is basically a tuple which allows reference to fields by name as well as index position. So it's a fixed structure of ordered named fields. It's also lightweight in that it uses slots to define field names thus eliminating the need to carry a dictionary in every instance.
A typical use case is to define a point:
from collections import namedtuple
Point = namedtuple("Point", "x y")
p1 = Point(x=11, y=22)
It's main drawback is that, being a tuple, it is immutable. But there is a method, replace which allows you to replace one or more fields with new values, but a new instance is created in the process.
There is also a mutable version of namedtuple available at ActiveState Python Recipes 576555 called records which permits direct field changes. I've used it and can vouch that it works well.
A dictionary is the classical way to do this in Python. It can't enforce that a value must exist though, and doesn't do initial values.
config = {'maxusers': 20, 'port': 2345, 'quota': 20480000}
collections.namedtuple() is another option in versions of Python that support it.