Intro to Python

minpower takes advantage of many of Python’s great features. If you have not worked with Python much this is a quick tutorial to help you understand what is going on in the code.

Syntax

Readability is prioritized, so no curly braces {} are used in Python. Instead code is indented to show loops, functions, …

List comprehension

Lists are basically arrays, but smarter:

>>> timesL=['1:00','2:00','3:00']
>>> for time in timesL: print(time)
1:00
2:00
3:00

Dictionaries

Dictionaries create one-to-one mappings, just what you would expect from the name:

>>> alphabetD=dict(a=1,b=2,c=3)
>>> print(alphabetD['a'])
1
>>> print(alphabetD['b'])
2

Key Word Arguments

Order does not matter. Defaults can be set:

>>> def f(a=0,b=2): return a+b
>>> print(f(), f(a=4), f(b=3), f(a=-1,b=2), f(b=3,a=4))
2 6 3 1 7

Object Oriented

Classes basically create a fixed set of attributes (properties) and methods (functionality). Objects of a certain class kind are called class instances. Instances are created via the classes name, with setup via the class’s __init__() method:

class Generator(object):
    """describes a generator"""
    def __init__(self,Pmin=0,Pmax=500, kind='coal'):
        #add attributes to class from inputs
        self.update(locals())
    def operating_range(self):
        """the power range of the generator"""
        return self.Pmax-self.Pmin
    #other methods here

So in practice this would look like:

>>> plantA = Generator(Pmin=10,Pmax=100)
>>> print(plantA.Pmin, plantA.kind, plantA.operating_range())
10 'coal' 90