Quickly Querying Swoop Objects¶
Swoop provides method chaining-based interface for querying and iterating over
EagleFilePart objects. If you are familiar with JQuery or LINQ query
language, then you have some experience with method-chaining interfaces.
Chained Mutators¶
To support chaining modifications to EagleFilePart objects, all the
mutators (or setter function) for EagleFilePart subclasses return the object so you can set
the location all the geometry information for a wire with the following:
from Swoop import *
w = Wire()
w.set_x1(4).set_y1(3).set_x2(2).set_y2(1)
Likewise you can add multiple wires to a package:
from Swoop import *
p = EagleFile.new_Package(). # Create a package
set_name("MY_PACKAGE"). # Set its name
add_wire(EagleFile.new_Wire(). # Create a Wire to add
set_layer("Top"). # set its layer
set_x1(4). # and it's start and end points
set_y1(3).
set_x2(2).
set_y2(1)).
add_wire(Wire(). # Create another Wire to add
set_layer("tDocu").
set_x1(1.1).
set_y1(2.3).
set_x2(2.4).
set_y2(9))
Working with Groups of EagelFilePart Objects¶
Searching for and iterating over EagelFilePart objects that meet some
criteria are common operations in Swoop. The From uses method
chaining to make this easier and more succint.
A From object contains a list of objects (usually, but
not necessarily, EagelFilePart objects). Calling a method on a
From object invokes the method on all the objects the From
object contains and returns a new From object containing the results.
If the method returns lists, the lists are concatenated. If it return dicts,
the values of the dicts are concatenated. Singleton values are appended into list.
If the method returns None, the value is ignored.
From defines several helper methods as well. For instance,
From.map() applies a function to item and returns a From object
containing the results and From.sorted() returns a sorted From.
All this allows for some very short implementations of complex iteration and searching. For instance, to print a list of all the packages in all the libraries of a file you could do:
from Swoop import *
print "\n".join(From(EagleFile.from_file("foo.sch")). # open the file, and create a From object for it
get_libraries(). # Get all the libraries
get_packages(). # Get all the packages from all the libraries
get_name(). # Get the names of those packages
sorted()) # Sort the result.
In addition, EagleFilePart and its subclasses provide several methods
built work with From objects. For example,
EagleFilePart.with_type() returns self if self is a
subclass of a give type. And the with_* methods allow for filtering
based on attribute values.)
The resulting library is quite powerful. For instance, to compute the total length of all the airwires in board:
from Swoop import *
from math import *
print "Total Airwire Length: "
print str(From(EagleFile.from_file("foo.brd")).
get_signals().
get_wires().
with_layer("Unrouted").
map(lambda w: sqrt(((w.get_x1()-w.get_x2())**2 + (w.get_y1()-w.get_y2())**2))).
reduce(lambda x,y:x + y))
The From Class¶
-
class
Swoop.From(*args)[source]¶ Bases:
objectAn ordered collection of (usually)
EagleFilePartobjects. Invoke a method on aFromobject, invokes the same method with the same arguments on all the objects it holds. The results are placed in a newFromobject that is returned.Fromobjects also provide several utility functions for accessing, filtering, mapping, and reducing their contents.You can combine the contents of the two
Fromobjects with the :code`+` operator.Fromobjects are iterable.-
append(l)[source]¶ Merge the contents of
linto this :class`From` object.Parameters: l – Python list of EagleFilePartobjects, aFromobject, or anEagleFilePartobject.Returns: This Fromobject with the new items added.Return type: From
-
apply(func)[source]¶ Apply a function to the elments of this
Fromand return aFromcontaining the results. Similar to the builtinmapfunction. A synonym forFrom.map().Parameters: func – The function to apply. Returns: A Fromcontaining the results.
-
filtered_by(func)[source]¶ Filter the
From. Similar to the builtinfiltermethod.Returns: A Fromobject contain the elements,xfor whichfunc(x)returnTrue
-
first()[source]¶ Return the first item in the
Fromobject. If theFromis empty, raise anIndexErrorexception.Returns: The first item in the FromReturn type: Varies Throws: IndexError
-
map(func)[source]¶ Apply a function to the elments of this
Fromand return aFromcontaining the results. Similar to the builtinmapfunction.Parameters: func – The function to apply. Returns: A Fromcontaining the results.
-
reduce(func, init=None)[source]¶ Reduce the elments of this
Fromand return the result. Similar to the builtinreducefunction.Parameters: - func – The function to apply.
- init – The starting value for the reduction.
Returns: The reduce result
Return type: Varies
-
sort(cmp=None, key=None, reverse=False)[source]¶ Sort the
Fromobject. Similar to the builtinsortedmethod (and takes the same arguments).Parameters: - cmp – Comparison function.
- key – A key accessor function.
- reverse – If
Truesort in reverse order.
Returns: A sorted
From
-