Debian Codegraph @

src/Programs/loop_example.py

raw symbols SCIP
1 #
2 # An example of how to output a subset of looped items.
3 #
4 from __future__ import print_function
5 import sys
6 from CifFile import CifFile, CifBlock
7 cf = CifFile("loop_example.cif")["some_stuff"] # open and parse our cif,
8 #we want data block named "some_stuff".
9 # --- Optional section
10 # Check that all our data
11 # items exist before attempting to access them
12 needed_items = [
13 "_atom_site_label",
14 "_atom_site_type_symbol",
15 "_atom_site_fract_x",
16 "_atom_site_fract_y",
17 "_atom_site_fract_z"]
18
19 loopkeys = cf.GetLoopNames("_atom_site_label") #get co-looped names
20 if len(filter(lambda a,b=loopkeys:a not in b, needed_items)) != 0:
21 print("Error: one or more items missing from atom_site_label loop")
22 sys.exit()
23 #
24 # ----- End of optional section
25
26 nb = CifBlock() # create a new block
27 map(lambda a:nb.AddItem(a,cf[a]),needed_items) #set new values
28 nb.CreateLoop(needed_items) # create the loop
29 df = CifFile() # create a new cif object
30 df.NewBlock("changed",nb) # and add our new block
31 outfile = open("updated.cif",'w') #open a file to write to
32 outfile.write (df.WriteOut(comment="# This file has been updated"))