Skip to content
Snippets Groups Projects
Commit 78f094ba authored by Marina Ganeva's avatar Marina Ganeva
Browse files

parse_header added to DNS data loader

parent f2b26872
No related merge requests found
import os, sys import os, sys, re
import numpy as np import numpy as np
import datetime import datetime
...@@ -71,21 +71,21 @@ class DNSdata: ...@@ -71,21 +71,21 @@ class DNSdata:
# parse each block # parse each block
# parse block 0 (header) # parse block 0 (header)
# [TODO:] rewrite to get rid of dependence on parse res = parse_header(blocks[0])
res = parse("# DNS Data userid={userid},exp={exp_id},file={run_number},sample={sample_name}", blocks[0]) #[TODO:]
# [TODO:] raise exception on the wrong file format #if not res: raise Exception "wrong file format" else
#if not res: try:
# print "Wrong file format." self.run_number = res['file']
# sys.exit() self.experiment_number = res['exp']
self.run_number = res['run_number'] self.sample_name = res['sample']
self.experiment_number = res['exp_id'] self.userid = res['userid']
self.sample_name = res['sample_name'] except:
self.userid = res['userid'] raise ValueError("The file %s does not contain valid DNS data format." % filename)
# parse block 1 (general information) # parse block 1 (general information)
b1splitted = map(str.strip, blocks[1].split('#')) b1splitted = map(str.strip, blocks[1].split('#'))
b1rest = [el for el in b1splitted] # otherwise unexpected behaviour due to the removed lines b1rest = [el for el in b1splitted] # otherwise unexpected behaviour due to the removed lines
#[TODO:] get rid of parse
for line in b1splitted: for line in b1splitted:
res = parse('User: {user_name}', line) res = parse('User: {user_name}', line)
if res: if res:
...@@ -183,7 +183,16 @@ class DNSdata: ...@@ -183,7 +183,16 @@ class DNSdata:
self.start_time = datetime.datetime.strptime(b7splitted[5], sinfmt).strftime(outfmt) self.start_time = datetime.datetime.strptime(b7splitted[5], sinfmt).strftime(outfmt)
self.end_time = datetime.datetime.strptime(b7splitted[6], einfmt).strftime(outfmt) self.end_time = datetime.datetime.strptime(b7splitted[6], einfmt).strftime(outfmt)
def parse_header(h):
"""
parses the header string and returns the parsed dictionary
"""
d = {}
regexp=re.compile("(\w+)=(\w+)")
result=regexp.finditer(h)
for r in result:
d[r.groups()[0]] = r.groups()[1]
return d
if __name__== '__main__': if __name__== '__main__':
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment