Keyword

This class is used for almost all Keywords. It simply saves and manipulates the lines of the Keyword smartly and also proides multiple means of manipulation. For reading these keywords, please enable the argument read_keywords in the KeyFile constructor.

class qd.cae.dyna.Keyword(lines, position=0)
Parameters
linesstr or list of str

Keyword data for initalization. May contain the lines of the keyword either as list or as one string. If provided one string, it is splitted at the line ending symbols.

positionint

line index at which the keyword shall be positioned. This will be the sorting index in a KeyFile.

Returns
keywordKeyword

Examples

>>> from qd.cae.dyna import Keyword
>>> 
>>> data = '''
>>> $------------------------------------------
>>> $ Parts, Sections, and Materials
>>> $------------------------------------------
>>> *PART
>>> $# title
>>> engine part number one 
>>> $#     pid     secid       mid     eosid   
>>>    2000001   2000001   2000017
>>> '''
>>>
>>> kw = Keyword(data)
>>> kw["pid"]
2000001
>>> kw[0]
'engine part number one '
>>> kw[1,0] = 2000002
Attributes
field_size

field_size

position

Notes —– Line index of the Keyword.

Methods

align

Type of alignment for comment names and fields:

append_line(line)

Parameters

get_card_valueByIndex(iCard, iField[, …])

Parameters

get_card_valueByName(name[, field_size])

Parameters

get_keyword_name()

Returns

get_line()

Parameters

get_lines()

Returns

has_long_fields()

Returns

insert_line(iLine, line)

Parameters

reformat_all([skip_cards])

Parameters

reformat_field(iCard, iField[, field_size, …])

Parameters

remove_line(iLine)

Parameters

set_card_valueByDict(fields[, field_size])

Parameters

set_card_valueByIndex(iCard, iField, value)

Parameters

set_card_valueByName(name, value[, field_size])

Parameters

set_line(iLine, line)

Parameters

set_lines(lines)

Parameters

switch_field_size([skip_cards])

Parameters

__init__(lines, position=0)

Keyword(lines, position=0)

Parameters
linesstr or list of str

Keyword data for initalization. May contain the lines of the keyword either as list or as one string. If provided one string, it is splitted at the line ending symbols.

positionint

line index at which the keyword shall be positioned. This will be the sorting index in a KeyFile.

Returns
keywordKeyword

Examples

>>> from qd.cae.dyna import Keyword
>>> 
>>> data = '''
>>> $------------------------------------------
>>> $ Parts, Sections, and Materials
>>> $------------------------------------------
>>> *PART
>>> $# title
>>> engine part number one 
>>> $#     pid     secid       mid     eosid   
>>>    2000001   2000001   2000017
>>> '''
>>>
>>> kw = Keyword(data)
>>> kw["pid"]
2000001
>>> kw[0]
'engine part number one '
>>> kw[1,0] = 2000002
__setitem__(args, value)

Please consider using ``set_card_valueByIndex`` or ``set_card_valueByName``

Parameters
argsstr, int or tuple
Possible arguments are:
  • card index (int)

  • card and field index (int,int)

  • card index, field index and field size (int,int,int)

  • field name (str)

  • field name and field size (str,int)

valueobject

Value to bet set. Must be convertible to string.

Raises
ValueError

if indexes are out of bounds or field name not found

RuntimeError

if arguments are not castable to int

Notes

This function is just a quick, dirty wrapper for the member function set_card_value. It is recommended to use set_card_value instead, since it’s much clearer.

Examples

>>> # keyword is from constructor example
>>>
>>> kw["title"] = Im too long hihihi
>>> kw["title"]
'Im too lon'
>>> kw["title",50] = Im too long hihihi
>>> kw["title"]
'Im too long hihihi'

Set value from indexes:

>>> kw[0] = "Set full keyword line to this"
>>> kw[1,0] = 2001 # pid
>>> kw[0,0,50] = "Set 50 chars to this"
__getitem__(arg1, arg2, field_size=0)

Abreviation for `get_card_valueByName` and `get_card_valueByIndex`

Parameters
arg1int or str

either card index (starting with 0) or field name

arg2int

if first argument is a card index, then this is the optional field index, otherwise full card will be returned

field_sizeint

the number of characters, which can be found in the card field.

Returns
entryint, float or str

field value

Raises
ValueError

if indexes are out of bounds or name not found

Notes

This function is just a quick, dirty wrapper for the member function get_card_value.

Examples

>>> # keyword is from constructor example
>>> kw["pid"]
2000001
>>> kw["yay"]
ValueError: Can not find field: yay in comments.
>>> kw[0]
'engine part number one '
>>> kw[0,0]
'engine par'
>>> kw[0,0,50]
'engine part number one'
__repr__()
Returns
reprstr

representation string of the keyword

Examples

>>> from qd.cae.dyna import KeyFile
>>> kf = KeyFile("path/to/keyfile")
>>> kf["*SET_NODE_ADD"]
[<Keyword: *SET_NODE_ADD>]
__iter__()
Returns
linestr

line of the keyword buffer

Examples

>>> for line in kw:
>>>    print(line)
__str__()
Returns
keyword_strstr

keyword as one string

Examples

>>> kw_as_string = str(kw)
>>> print(kw)
class align
Type of alignment for comment names and fields:
  • left

  • right

  • middle

Examples

>>> # The alignment is always global
>>> Keyword.field_alignment = Keyword.align.middle
append_line(line)
Parameters
linestr

line to append to the internal string buffer

Examples

>>> kw.append_line("$ Im a comment")
property field_size
Raises
ValueError

if the library doesn’t like you for setting a negative field size.

Notes

Size of the fields in the keyword. Most cards have a field size of 10. Unfortunately some cards have a deviating field size (e.g. 8) and there is no automatic way to check for this.

Examples

>>> # the field size of the card
>>> kw.field_size
10
>>> # some keywords use 8 chars per field
>>> kw.field_size=8
get_card_valueByIndex(iCard, iField, field_size=0)
Parameters
iCardint

card index

iFieldint

field index

field_sizeint

number of chars to read

Returns
entryint, float or str

field value

Raises
ValueError

if indexes are out of bounds

Examples

>>> # keyword is from constructor example
>>> # get pid
>>> kw.get_card_value(1,0) 
2000001
>>> kw.get_card_value(0) 
'engine part number one '
>>> kw.get_card_value(0,0)
'engine par'
>>> kw.get_card_value(0,0,50) 
'engine part number one'
get_card_valueByName(name, field_size=0)
Parameters
namestr

name of the field in comments

field_sizeint

number of chars to read

Returns
entryint, float or str

field value

Raises
ValueError

if name not found in the previous comment line

Examples

>>> # keyword is from constructor example
>>> # get pid
>>> kw.get_card_value("pid") 
2000001
get_keyword_name()
Returns
keyword_namestr
Raises
RuntimeError

if keyword definition can not be found in line buffer

Notes

According to LS-Dyna, char 0 in the line must be a *

Examples

>>> kw.get_keyword_name()
'*PART'
get_line()
Parameters
iLineint

index of the line

Returns
linestr
Raises
ValueError

if iLine out of bounds

Examples

>>> kw.get_line(4)
'$# title'
get_lines()
Returns
lineslist of str

lines of the keyword

Examples

>>> list_of_lines = kw.get_lines()
has_long_fields()
Returns
has_long_fieldsbool

whether the card uses double size fields or not

Raises
RuntimeError

if keyword definition can not be found in line buffer

Notes

A card uses double sized fields, if + is appended to the keywords name.

Examples

>>> kw.has_long_fields()
False
>>> kw.get_keyword_name()
'*PART'
insert_line(iLine, line)
Parameters
iLineint

index of the line

linestr

Examples

>>> kw.insert_line(4, "$ comment")
property position

Notes

Line index of the Keyword. This is used for sorting before writing to a file.

Examples

>>> kw.position = 438
>>> kw.position
438
reformat_all(skip_cards=[])
Parameters
skip_cardslist of int

indexes of cards not to touch

Notes

This function reformats the card regarding the global formatting rules. BEWARE skip cards which have a field with unnormal size.

Examples

>>> Keyword.name_delimiter_used = True
>>> Keyword.name_delimiter = '|'
>>> Keyword.name_spacer = '-'
>>> Keyword.name_alignment = Keyword.align.right
>>> Keyword.field_alignment = Keyword.align.right
>>> kw.reformat_all(skip_cards=[0])
>>> print(kw)
$------------------------------------------
$ Parts, Sections, and Materials
$------------------------------------------
*PART
$# title
engine part number one 
$------pid|----secid|------mid|----eosid|
       100       200   2000017
reformat_field(iCard, iField, field_size=0, format_field=True, format_name=True)
Parameters
iCardint

card index

iFieldint

field index

field_sizeint

size of field in chars

format_fieldbool

whether to format the field

format_namebool

whether to format the comment name

Raises
ValueError

if card and field index not found

Notes

This function reformats a single card regarding the global formatting rules.

Examples

>>> Keyword.name_delimiter_used = True
>>> Keyword.name_delimiter = '|'
>>> Keyword.name_spacer = '-'
>>> Keyword.name_alignment = Keyword.align.right
>>> Keyword.field_alignment = Keyword.align.right
>>> kw.reformat_field(0,0,40)
>>> print(kw)
$------------------------------------------
$ Parts, Sections, and Materials
$------------------------------------------
*PART
$----------------------------------title
                  engine part number one
$#     pid     secid       mid     eosid   
       100       200   2000017
remove_line(iLine)
Parameters
iLineint

Examples

>>> kw.remove_line(4)
set_card_valueByDict(fields, field_size=0)
Parameters
fielddict

fields to set, key can be string or indexes

field_sizeint

number of chars to read

Raises
ValueError

if name or card/field indexes not found

Examples

>>> # keyword is from constructor example
>>>
>>> fields = {"pid":100, (1,0):200}
>>> kw.set_card_valueByDict(fields)
>>> print(kw)
$------------------------------------------
$ Parts, Sections, and Materials
$------------------------------------------
*PART
$# title
engine part number one 
$#     pid     secid       mid     eosid   
       100       200   2000017
set_card_valueByIndex(iCard, iField, value, name="", field_size=0)
Parameters
iCardint

card index

iFieldint

field index

valueobject

Value to bet set. Must be convertible to string.

namestr

name in comments to be set

field_sizeint

number of chars to read

Raises
ValueError

if indexes are out of bounds

Examples

>>> # keyword is from constructor example
>>>
>>> # set pid
>>> kw.set_card_valueByIndex(1, 0, value=100, name="haha")
>>> print(kw)
$------------------------------------------
$ Parts, Sections, and Materials
$------------------------------------------
*PART
$# title
engine part number one 
$#    haha     secid       mid     eosid   
       100   2000001   2000017
set_card_valueByName(name, value, field_size=0)
Parameters
namestr

name of the field in comments

valueobject

Value to bet set. Must be convertible to string.

field_sizeint

number of chars to read

Raises
ValueError

if name not found in the previous comment line

Examples

>>> # keyword is from constructor example
>>>
>>> # set pid
>>> kw.set_card_valueByName("pid", value=100)
>>> print(kw)
$------------------------------------------
$ Parts, Sections, and Materials
$------------------------------------------
*PART
$# title
engine part number one 
$#     pid     secid       mid     eosid   
       100   2000001   2000017
set_line(iLine, line)
Parameters
iLineint

index of the line

linestr

Examples

>>> kw.set_line(4, "blubber")
set_lines(lines)
Parameters
lineslist of str

set the lines of the line buffer

Examples

>>> lines = ["*PART","engine part number one"]
>>> kw.set_lines(lines)
switch_field_size(skip_cards=[])
Parameters
skip_cardslist of int

indexes of cards not to touch

Notes

This function switches the card size between single and double sized fields. In the process the global formatting rules are applied. BEWARE skip cards which have a field with unnormal size.

Examples

>>> kw.switch_field_size(skip_cards=[0])