bulkdata

Subpackages

Submodules

bulkdata.card

The card module provides classes related to the creation and manipulation of bulk data card objects. It contains the Card class that facilitates the conversion of collections of field values to and from bulk data card formatted strings.

class bulkdata.card.Card(name=None, size=0)[source]

Card class allows the user to create and modify bulk data cards.

Parameters:
  • name – The name of the card
  • size – The number of initial blank fields, defaults to 0
__bool__()[source]

Return True if the card contains any fields, False otherwise.

__delitem__(key)[source]

Delete the field item from card.

Parameters:key (int, slice) – The indexing key denoting which field(s) to delete
__getitem__(key)[source]

Get field item from the card.

Parameters:key (int, slice, list) – The indexing key denoting which field(s) to get
Returns:The field value(s)
__iter__()[source]

Iterate through the fields in the card.

__len__()[source]

Return number of fields in the card.

__setitem__(key, value)[source]

Set field item into the card.

Parameters:
  • key (int, slice, list) – The indexing key denoting where to set the field(s)
  • value – The field value(s) to set
__str__()[source]

Dump the card to as a bulk data card string with default format.

Returns:The bulk data card string
append(value, fieldspan=1)[source]

Append a field value to card fields.

Parameters:
  • value – The field value
  • fieldspan – The number of field cells the value spans, defaults to 1
dumps(format='fixed')[source]

Dump the card to bulk data formatted string.

Parameters:format – the desired format, can be one of: [“free”, “fixed”], defaults to “fixed”
Returns:The bulk data card string representation
extend(values, fieldspan=1)[source]

Extend card fields with sequence of field values.

Parameters:
  • values – The sequence of field values
  • fieldspan – The number of field cells that each value spans, defaults to 1
fields

The card fields.

get_large(key)[source]

Get field value spanning multiple fields. This gets the same fields as __getitem__() but it joins them before converting to a single large value.

Parameters:key – The indexing object denoting which field(s) to get
Returns:The large field value
classmethod loads(card_str)[source]

Load Card object from a bulk data card string.

Parameters:card_str – the bulk data card string
Returns:The loaded Card object
name

The card name.

pop()[source]

Remove the last field.

resize(size)[source]

Resize the card fields to contain size fields. If current number of fields is greater than size, the extra fields will be removed from the end. If number of fields is less than size, extra blank fields are appended.

Parameters:size – The desired number of fields
set_raw_fields(fields)[source]

Set the fields directly, without internal conversion of fields values to Field objects.

Note

The user should avoid using this function unless he/she knows what they are doing.

strip()[source]

Remove any trailing blank fields.

values()[source]

Get a list of the values of the card fields.

bulkdata.cli

Console script for bulkdata. (Currently does nothing)

bulkdata.deck

The deck module provides the Deck class.

class bulkdata.deck.Deck(cards=None, header=None)[source]

Deck class allows the user to load and update bulk data files, loading the bulk data cards into Card objects.

Parameters:
  • cards – initialize the deck with these cards, defaults to None.
  • header – the header, which is prepended to the bulk data section when dumping the deck, defaults to None.
__bool__()[source]

Return True if the deck contains any cards, False otherwise.

__getitem__(key)[source]

Get card(s) in the deck.

Parameters:key (int, slice, list, str) – The indexing key denoting where to get the card(s)
Returns:The card(s)

If key is of type str, this method returns all cards with name key.

__iter__()[source]

Iterate through the cards in the deck.

__len__()[source]

Return number of cards in the deck.

__setitem__(key, value)[source]

Set card(s) in the deck.

Parameters:
  • key (int, slice, list) – The indexing key denoting where to set the card(s)
  • value – The card(s) to set
__str__()[source]

Dump the deck to as a bulk data string with default format

Returns:The bulk data string
append(card)[source]

Append a card to the deck.

Parameters:card – The card to append
cards

The deck cards.

delete(filter=None)[source]

Delete cards matching the query denoted by filter.

Parameters:filter (dict, str) – Specifies which cards to delete.
Returns:The number of cards deleted.
dump(fp, format='fixed')[source]

Dump the deck to a bulk data file.

Parameters:
  • fp – The bulk data file object
  • format – The desired format, can be one of: [“free”, “fixed”], defaults to “fixed”
dumps(format='fixed')[source]

Dump the deck to a bulk data string.

Parameters:format – The desired format, can be one of: [“free”, “fixed”], defaults to “fixed”
Returns:The bulk data string
extend(cards)[source]

Extend deck cards with sequence of cards.

Parameters:cards – The sequence of cards
find(filter=None)[source]

Find cards matching the query denoted by filter.

Parameters:filter (dict, str) – Specifies which cards to find.
Returns:A generator object iterating through every card matching the filter.

If filter is a dict, there are three keywords that may be used.

  • name, str: Filter for cards with matching name.
  • fields, dict: Given an “index” and “field” member, filter cards where the field(s) at index index, has/have value field.
  • contains: Filter for cards containing a field that matches the contains value, or any contains value if contains is a list.

The following code block is an example of using find with a filter dict:

filter_ = {
    # name is ASET1
    "name": "ASET1",
    
    "fields": {
        
        # first field
        "index": 0,
        
        # with value 3
        "value": 3
    },
    
    # contains values 1 and "THRU"
    "contains": [1, "THRU"] 
}

card = next(deck.find(filter_))
print(card)
ASET1   3       1       THRU    8

If filter is a str, the filter will match cards with name matching filter.

card = next(deck.find("AERO"))
print(card)
AERO    3       1.3     100.    .00001  1       -1
find_one(filter=None)[source]

Find the first card matching the query denoted by filter.

Parameters:filter (dict, str) – Specifies which card to find.
Returns:The first matching card, or None if no match is found.
classmethod load(fp)[source]

Load Deck object from a bulk data file object.

Parameters:fp – The bulk data file object
Returns:The loaded Deck object
classmethod loads(deck_str)[source]

Load Deck object from a bulk data string.

Parameters:deck_str – The bulk data string
Returns:The loaded Deck object
replace(filter, card)[source]

Replace cards matching the query denoted by filter with card.

Parameters:
  • filter (dict, str) – Specifies which cards to replace.
  • card – The replacement card
replace_one(filter, card)[source]

Replace the first card matching the query denoted by filter with card.

Parameters:
  • filter (dict, str) – Specifies which card to replace.
  • card – The replacement card
Returns:

The replacement card, or None if no match is found.

sorted(key=None, reverse=False)[source]

Return a deck containing the sorted deck cards.

Parameters:
  • key – Specifies a function of one argument that is used to extract a comparison key from each card. If None, the cards will be sorted by name.
  • reverse – Boolean value. If set to True, then the cards are sorted as if each comparison were reversed.
Returns:

The Deck object containing the sorted cards.

update(filter, update)[source]

Update cards matching the query denoted by filter with changes denoted by update dict, which has the same keyword options as the fields dict in filter.

Note

Avoid this function as there is no current tutorial, has not been well tested, and does not appear to add any functionality not achieved otherwise.

Parameters:
  • filter (dict, str) – Specifies which cards to replace.
  • card – The replacement card

bulkdata.error

exception bulkdata.error.EmptyLineError[source]
exception bulkdata.error.Error[source]

bulkdata.field

class bulkdata.field.Field(value, fieldspan=1)[source]
is_blank()[source]
value
width
class bulkdata.field.LargeField(value, fieldspan=2)[source]
classmethod join(fields)[source]
split(fieldspan=1)[source]

Split into Fields with given fieldspan

bulkdata.field.is_integer_field(field)[source]
bulkdata.field.is_real_field(field)[source]
bulkdata.field.read_field(field)[source]

Convert field string to value

bulkdata.field.read_integer_field(field)[source]
bulkdata.field.read_real_field(field)[source]
bulkdata.field.write_field(value, fieldspan=1)[source]

Convert value to field string

bulkdata.format

class bulkdata.format.BaseFormatter[source]
continuation(index='')[source]
delimiter = ''
endofline(index)[source]
fieldwidth = 8
format_card(card)[source]
format_field(field)[source]
newline = '\n'
remove_trailing_blanks(fields)[source]
valuesperline = 8
class bulkdata.format.FixedFormatter[source]
format_field(field)[source]
class bulkdata.format.FreeFormatter[source]
delimiter = ','
bulkdata.format.format_card(card, format=None)[source]

bulkdata.parse

class bulkdata.parse.BDFParser(bdf_str)[source]
BEGINBULK = 'BEGIN BULK'
ENDDATA = 'ENDDATA'
FIELDSPERBODY = 8
FIELDSPERLINE = 10
FIELDWIDTH = 8
MAXLINELENGTH = 80
current_line()[source]
endofbdf()[source]
ignore_enddata(bdf_str)[source]
increment_line(i=1)[source]
is_comment(line)[source]
is_line_free(line)[source]
parse()[source]
parse_card()[source]
parse_fields(fields)[source]
parse_header()[source]
parse_line(line)[source]
parse_line_fixed(line)[source]
parse_line_free(line)[source]
remove_comments()[source]

bulkdata.util

bulkdata.util.islist(value)[source]
bulkdata.util.repr_list(list_)[source]
bulkdata.util.split_fields(fields_str, fieldwidth=8)[source]

Split fields_str into fields of length fieldwidth

Module

Top-level package for Bulk Data Python Package.

The Card and Deck classes are accessible via the top-level bulkdata module.