pylib.data module

Read and write data to or from file and manipulate data structures.

Date

2019-12-28

find_last(sequence, pattern)[source]

Find last last occurance in sequence (text)

Parameters
  • sequence (str) – text to search in

  • pattern (str) – search pattern

Returns

index (-1 if pattern not in sequence)

Return type

int

fold_list(lst, n)[source]

Convert one-dimensional kx1 array (list) to two-dimensional mxn array. m = k / n

Parameters
  • lst (list) – list to convert

  • n (int) – length of the second dimenson

Returns

two-dimensional array (list of lists)

Return type

list

get_id(ids, uide)[source]

Get full id from unique id ending.

Parameters
  • ids (list) – ids

  • uide (str) – unique id ending

Returns

full id

Return type

str or int

issequence(obj)[source]

True for tuple, list, str False for int, dict, set

Example

>>> issequence(())
True
>>> issequence((3, ))
True
>>> issequence([])
True
>>> issequence([1])
True
>>> issequence([1, 2])
True
>>> issequence('')
True
>>> issequence((3))
False
>>> issequence({})
False
>>> issequence(set())
False
load(file_name, default=None, verbose=False)[source]

Load stored program objects from binary file.

Parameters
  • file_name (str) – file to load

  • default (object) – return object if data loading fails

  • verbose (bool) – verbose information (default = False)

Returns

loaded data

Return type

object

print_list(lst)[source]

Print list, one list element per line.

Parameters

lst (list) – list to print

read(file_name)[source]

Read ascii data file.

Parameters

filename (str) – file to read

Returns

file content

Return type

str

read_columns(file_name, x_column, y_column, default=None, verbose=False)[source]

Read ascii data file.

Parameters
  • file_name (str) – file to read

  • x_column (int) – column index for the x data (first column is 0)

  • y_column (int) – column index for the y data (first column is 0)

  • default (object) – return object if data loading fails

  • verbose (bool) – verbose information (default = False)

Returns

x and y

Return type

tuple(list, list)

seq(start, stop=None, step=1)[source]

Create an arithmetic bounded sequence.

The sequence is one of the following;

  • empty \(\{\}=\emptyset\), if start and stop are the same

  • degenerate \(\{a\}\), if the sequence has only one element.

  • left-close and right-open \([a, b)\)

Parameters
  • start (int or float) – start of the sequence, the lower bound. If only start is given than it is interpreted as stop and start will be 0.

  • stop (int or float) – stop of sequence, the upper bound.

  • step (int or float) – step size, the common difference (constant difference between consecutive terms).

Returns

arithmetic bounded sequence

Return type

list

store(file_name, object_data)[source]

Store program objects to binary file.

Parameters
  • file_name (str) – file to store

  • object_data (object) – data to store

str_between(text, left, right)[source]

Get text between two pattern.

Text can be multi-line.

Parameters
  • text (str) – text to search in

  • left (str) – left pattern

  • right (str) – right pattern

Returns

text between the left and right pattern

Return type

str

str_to_list(string, delimiter=';\n', newline_replacement='')[source]

Converts a string with block information into a list.

This function un-wraps multi-line block information into one line.

Parameters
  • string (str) – string with block information

  • delimiter (str) – block delimiter (default = ‘;n’). This will be removed from the resulting list.

  • newline_replacement (str) – block lines replacement (default = ‘’)

Returns

list of block information

Return type

list

Note

Every line is left striped. Empty line are ignored.

Example

before (string):
  FILE_DESCRIPTION(('Open CASCADE Model'),'2;1');
  FILE_NAME('Open CASCADE Shape Model','2019-10-14T14:32:20',('Author'),(
      'Open CASCADE'),'Open CASCADE STEP processor 7.1','Open CASCADE 7.1'
    ,'Unknown');
  FILE_SCHEMA(('AUTOMOTIVE_DESIGN { 1 0 10303 214 1 1 1 1 }'));

after (list elements one per line):
  FILE_DESCRIPTION(('Open CASCADE Model'),'2;1')
  FILE_NAME('Open CASCADE Shape Model','2019-10-14T14:32:20',('Author'),('Open CASCADE'),'Open CASCADE STEP processor 7.1','Open CASCADE 7.1','Unknown')
  FILE_SCHEMA(('AUTOMOTIVE_DESIGN { 1 0 10303 214 1 1 1 1 }'))
unique_ending(ids, n=1)[source]

From id list get list with unique ending.

Parameters
  • ids (list) – ids

  • n (int) – minumum chars or ints

Returns

unique ending of ids

Return type

list

unique_list(sequence)[source]
unique_list_hashable(sequence)[source]

faster using set() but elements of the sequence must be hashable. unhashable types: ‘list’

write(file_name, data)[source]

Write ascii file.

Parameters
  • file_name (str) – file to write

  • data (str) – data to write