
0
Extension for MMpy.File class
Good afternoon.
Since I've been working in Micromine, I've been creating and changing different extensions for existing objects, that's why I decided to share with you one of them that is really convenient from my point of view. New class is an extension for MMpy.File and named File, contains some new methods and all methods that are in MMpy.File. Seventeen new methods with description are below:
File class supports complex indexes:
Since I've been working in Micromine, I've been creating and changing different extensions for existing objects, that's why I decided to share with you one of them that is really convenient from my point of view. New class is an extension for MMpy.File and named File, contains some new methods and all methods that are in MMpy.File. Seventeen new methods with description are below:
- add_columns (structure) adds column(-s) according to specified structure. Type of structure object must be dictionary (see 15).
f = File() f.open(path) header, struct = ["CODE1", "CODE2"], {} for col in header: struct[col] = {"width": 10, "prec": 0, "type": "character"} f.add_columns(struct) f.close()
- append (array) adds array to the end of a file. Array may consist of one or more nested arrays.
f = File() f.open(MMpy.Project.path() + "test.dat") f.append([[0,1], [2,1]) f.close()
- create (path, header, structure) is a static method that do not require creating of File object. It creates a Micromine according to specified structure. Header is need for determining a column order.
path = MMpy.Project.path() + "test.dat" header, struct = ["X", "Y"], {} for col in header: struct[col] = {"width": 0, "prec": 3, "type": "real"} File.create(path, header, struct)
- create_from_csv (datpath, csvpath, header_line, fr, to, delimiter) is a static method that do not require creating of File object. It creates Micromine DAT file froma CSV file. CSV file will be read from line fr to line to. Header_line is a number of record that will be a header of a new DAT file. Delimiter is set to ";" by default.
File.create_from_csv(path, "C:\\MMProject\\test.csv")
- delete_column (column) drops columns from a file.
f = File() f.open(MMpy.Project.path() + "test.STR") f.delete_column("STRING") f.close()
- get_header (path) is a static method that do not require creating of File object. It returns a file header.
print (File.get_header(MMpy.Project.path() + "test.STR")) >>>> ['EAST', 'NORTH', 'RL', 'STRING', 'JOIN']
- get_struct (path) is a static method that do not require creating of File object. It returns a file structure
print (File.get_struct(MMpy.Project.path() + "test.STR") >>>> {'RL': {'type': 'real', 'width': 8, 'prec': 6}, 'EAST': {'type': 'real', 'width': 8, 'prec': 6}, 'NORTH': {'type': 'real', 'width': 8, 'prec': 6}, 'JOIN': {'type': 'real', 'width': 8, 'prec': 0}, 'STRING': {'type': 'character', 'width': 6, 'prec': 0}}
- header returns a file header
f = File() f.open(MMpy.Project.path() + "test.STR") print (f.header) f.close() >>>> ['EAST', 'NORTH', 'RL', 'STRING', 'JOIN']
- insert (index, array) inserts a row to file.
f = File() f.open(MMpy.Project.path() + "test.STR") f.insert(4, [0,1,2,"",3]) f.close()
- read (path, columns, fr, to, asstr) is a static method that do not require creating of File object. It reads a file. You can choose columns that you want to read and specify range of line that will be read. If you set asstr to True then all data will be read as strings. It's set to False by default. All parameters except path are optional.
path = MMpy.Project.path() + "test.STR" # read all data from file data = File.read(path) # or read data from EAST and NORTH columns from 10 to 15 lines data = File.read(path, ["EAST", "NORTH"], 10, 15)
- rename (oldname, newname) renames column
f = File()<br/>f.open(MMpy.Project.path() + "test.STR") f.rename("EAST", "X") f.close()
- set_precision (column, precision) sets precision to a column.
f = File() f.open(MMpy.Project.path() + "test.STR") f.set_type("EAST", 3) f.close()
- set_type (column, type) sets type to a column. You can choose one of the following column types: "numeric", "character", "real", "float", "short", "long"
f = File() f.open(MMpy.Project.path() + "test.STR") f.set_type("EAST", "float") f.close()
- set_width (column, width) sets width to a column.
f = File() f.open(MMpy.Project.path() + "test.STR") f.set_width("STRING", 50) f.close()
- struct returns a nested dictionary of the following structure: {column_name: {"width": , "prec": , "type": }}. You can choose one of the following column types: "numeric", "character", "real", "float", "short", "long".
f = File() f.open(MMpy.Project.path() + "test.STR") print (f.struct) f.close() >>>> {'RL': {'type': 'real', 'width': 8, 'prec': 6}, 'EAST': {'type': 'real', 'width': 8, 'prec': 6}, 'NORTH': {'type': 'real', 'width': 8, 'prec': 6}, 'JOIN': {'type': 'real', 'width': 8, 'prec': 0}, 'STRING': {'type': 'character', 'width': 6, 'prec': 0}}
- to_csv (path, delimiter) exports data to CSV with specified delimiter. Delimiter is ";" by default
f = File() f.open(MMpy.Project.path() + "test.STR") f.to_csv("C:\\MMProject\\test.csv") f.close()
- write (path, data, header, structure, nrows) is a static method that do not require creating of File object. Writes data to Micromine file. Structure and nrows are optional parameters. If file doesn't exist then it will be created. Parameter nrows is a number of lines that are used for determining an array MM structure.
File.write(MMpy.Project.path() + "test.STR", [[0,0,0,"",1]], ["X", "Y", "Z", "STRING", "JOIN"])
File class supports complex indexes:
f = File() f.open(MMpy.Project.path() + "test.STR") print (f[0]) # returns first record in file print (f[1:3]) # returns from first to third record in file print (f[::2]) # returns each second line in file print (f["EAST"]) # returns data from EAST column print (f["EAST", "NORTH"]) # returns data from EAST and NORTH columns print (f["EAST", "NORTH", ::2) # returns each second record from EAST,NORTH for row in f: print (row) # returns all records f.close()You can also write values to file using operator '='. It's possible to assign values to column, to line and to cell.
f = File() f.open(MMpy.Project.path() + "test.STR") f["EAST"] = 1 # sets 1 to column EAST f[2] = [1,2,3,"",1] # sets [1,2,3,"",1] to second record f["STRING", 2] = "LINE1" # sets LINE1 to column STRING, second record f["JOIN] = [1,2] # sets [1,2] to JOIN, repeats if the end isn't reached f.close()You can download the script here. I'm glad if you find this script useful. If you find any errors, please write about them here.
Customer support service by UserEcho