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:
object
An ordered collection of (usually)
EagleFilePart
objects. Invoke a method on aFrom
object, invokes the same method with the same arguments on all the objects it holds. The results are placed in a newFrom
object that is returned.From
objects also provide several utility functions for accessing, filtering, mapping, and reducing their contents.You can combine the contents of the two
From
objects with the :code`+` operator.From
objects are iterable.-
append
(l)[source]¶ Merge the contents of
l
into this :class`From` object.Parameters: l – Python list of EagleFilePart
objects, aFrom
object, or anEagleFilePart
object.Returns: This From
object with the new items added.Return type: From
-
apply
(func)[source]¶ Apply a function to the elments of this
From
and return aFrom
containing the results. Similar to the builtinmap
function. A synonym forFrom.map()
.Parameters: func – The function to apply. Returns: A From
containing the results.
-
filtered_by
(func)[source]¶ Filter the
From
. Similar to the builtinfilter
method.Returns: A From
object contain the elements,x
for whichfunc(x)
returnTrue
-
first
()[source]¶ Return the first item in the
From
object. If theFrom
is empty, raise anIndexError
exception.Returns: The first item in the From
Return type: Varies Throws: IndexError
-
map
(func)[source]¶ Apply a function to the elments of this
From
and return aFrom
containing the results. Similar to the builtinmap
function.Parameters: func – The function to apply. Returns: A From
containing the results.
-
reduce
(func, init=None)[source]¶ Reduce the elments of this
From
and return the result. Similar to the builtinreduce
function.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
From
object. Similar to the builtinsorted
method (and takes the same arguments).Parameters: - cmp – Comparison function.
- key – A key accessor function.
- reverse – If
True
sort in reverse order.
Returns: A sorted
From
-