Kickstart Python For Revit API (2)

, ,
Python Blue Logo

After a brief introduction to some of Python’s basic elements, this post will introduce some more handy elements to use in Dynamo and it will serve you well in any other application that uses Python.

Python data types and data structures:

A continuation of the basic data types, here is some new elements that allow data to be organized. Imagine that you have to collect all the windows’ Mark parameter in a huge project; all the data retrieved won´t be that useful if it is not organized somehow. There are a few structures to do this:

Data type in Python Example Data type in Dynamo Example
List [“cat”, 24, “78”] List [“Dynamo”, 33]
Tuple (“cat”, 24, “78”)
Dictionary {“cat” : 238, “piggie” : “88”} Dictionary {“doggies” : 78, “cuteKittie” : false}

Lists are how data are organized by default in Dynamo. If you retrieve any parameter, the output of the node in Dynamo will return a list. Lists are a type of data structure that keeps the order of the elements in it unless changed. In programming, the first element is numbered as 0, called index. So if a list contains 8 elements, the indexes will run from 0 to 7.

To access an element in a list in Python, use an index inside the [] (square brackets) to call that element:

exampleList = [“cat”, 24, “78”]
exampleList[0]
"cat"

If you want to replace an element inside a list, it can be done by selecting it and assign it a new value:

exampleList[0] = “dog”
exampleList
[“dog”, 24, “78”]

In code blocks, prior to Dynamo 2.0, lists are defined with {} (curly brackets). Since Dynamo 2.0, lists took the conventional definition of using [] (square brackets).

Tuples are like lists, data structures that store the elements in order but with a catch, they cannot be modified. This means that if you need to replace an element inside a tuple, you must convert the tuple to a list. The only reason to use tuples is that they run faster than lists (it makes sense, Python knows that they cannot be changed, so it won´t keep track of them).

As far as I have tested, there is no node to create tuples in Dynamo, but it is an easy task using Code blocks or a Python Script. Or just create your own customized node.

Dictionaries are data structures that are defined by a key, like an index, and the value associated with that key. Keys must be in quotation marks and values can be of any data type.

Dicts are not ordered data structures and have a benefit over lists, no matter how many elements it contains; it will still run at the same speed when an action is called upon them. Suppose that you want to find a specific element inside a list, the program will check one element at a time to match the searched one. If the list contains a billion elements, it will slow down the entire program. This doesn´t happen to dictionaries and it is one of the reasons to use them. Another reason to use dictionaries is their ability to be sorted by keys, overcoming the limitations of lists of using just integers as indexes.

exampleDict = {“plans” : 456, "sections" : 43, "print" : False}
exampleDict["sections"]
43

In Dynamo, dictionaries are created by the node Dictionary.ByKeysValues node and in code blocks are created with {} from Dynamo 2.0. Previous versions of Dynamo had both lists and dicts merged.

Python functions:

Functions are a sequence of statements that performs a computation. It is defined by a name and the set of statements. With classes, this is the way easiest to write code that can be reused later in the same or different programs.

Functions are handy because they allow you to write code once and reused it several times, involving future code changes in only one place, rather than scattered changes along the program. Once defined, can be called or invoked as many times as needed to do the same job.

In addition, functions can be composed, meaning a function can call another function as its argument and they are the base for the functional programming. This a paradigm in programming that its foundation is to avoid modifying mutable data, a much less prone to errors and less debugging way to program.

The basic syntax for functions is as following:

def functionName(parameters):

          Body of the function

return something # Not required, but if not specified it will return None
def multiplication(a, b)

          value = a * b

          return value
multiplication(7, 8)
56

The function multiplication takes two parameters, called arguments once you invoke the function. It creates a local variable called value that stores the multiplication value of the two parameters and it is only accessible inside the function. After the assignment of the result to the variable, it returns it. Once invoked, the function multiplication with 7 and 8 as arguments, will return 56 as expected.

Leave a Reply

Your email address will not be published. Required fields are marked *