CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Classes | Functions | Variables
utils Namespace Reference

Classes

class  BinToBin
 
class  BinToBin1percent
 
class  Chi2
 
class  KS
 
class  StatisticalTest
 
class  unpickler
 

Functions

def ask_ok
 
def code_generator
 
def friendly_since
 
def functor
 
def get_relval_cmssw_version
 
def get_relval_id
 
def get_relval_max_version
 
def get_relval_version
 -------------—— Make files pairs: RelVal utils ---------------—— More...
 
def get_relvaldata_cmssw_version
 
def get_relvaldata_id
 -----------—— Make files pairs: RelValData utils --------------—— More...
 
def get_relvaldata_max_version
 
def get_relvaldata_version
 
def getNbins
 
def is_empty
 
def is_relvaldata
 ----------------------— Make files pairs -----------------------— More...
 
def is_sparse
 
def literal2root
 
def logger
 
def make_files_pairs
 
def parse_word
 
def profile2histo
 
def setTDRStyle
 
def template_directory
 
def test_env
 
def to_datetime
 
def to_timestamp
 
def tree
 
def user_info
 
def wget
 

Variables

int _log_level = 10
 
tuple TAG = re.compile(r'[a-zA-Z0-9]')
 
 theargv = sys.argv
 

Detailed Description

File that contains utility functions used by various modules, but that do not fit into any single module.

Function Documentation

def utils.ask_ok (   prompt,
  retries = 4,
  complaint = 'yes or no' 
)

Definition at line 431 of file utils.py.

References print().

432 def ask_ok(prompt, retries=4, complaint='yes or no'):
433  while True:
434  ok = raw_input(prompt)
435  if ok in ('y', 'ye', 'yes'):
436  return True
437  if ok in ('n', 'no'):
438  return False
439  retries = retries - 1
440  if retries < 0:
441  raise IOError('refusenik user')
442  print(complaint)
443 
444 #-------------------------------------------------------------------------------
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def ask_ok
Definition: utils.py:431
def utils.code_generator (   kwds)
Code generator function, parse user arguments, load and
return appropriate template generator module.

Definition at line 122 of file utils.py.

References print(), and str.

Referenced by cms.generate(), and main.generator().

123 def code_generator(kwds):
124  """
125  Code generator function, parse user arguments, load and
126  return appropriate template generator module.
127  """
128  debug = kwds.get('debug', None)
129  if debug:
130  print("Configuration:")
131  pprint.pprint(kwds)
132  try:
133  klass = kwds.get('tmpl')
134  mname = 'FWCore.Skeletons.%s' % klass.lower()
135  module = __import__(mname, fromlist=[klass])
136  except ImportError as err:
137  klass = 'AbstractPkg'
138  module = __import__('FWCore.Skeletons.pkg', fromlist=[klass])
139  if debug:
140  print("%s, will use %s" % (str(err), klass))
141  obj = getattr(module, klass)(kwds)
142  return obj
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def code_generator
Definition: utils.py:122
#define str(s)
def utils.friendly_since (   time_type,
  since 
)
Takes a since and, if it is Run-based expressed as Lumi-based,
returns the run number.
Otherwise, returns the since without transformations.

Definition at line 18 of file utils.py.

18 
19 def friendly_since(time_type, since):
20  """
21  Takes a since and, if it is Run-based expressed as Lumi-based,
22  returns the run number.
23  Otherwise, returns the since without transformations.
24  """
25  if time_type == "Run" and (since & 0xffffff) == 0:
26  return since >> 32
27  else:
return since
def friendly_since
Definition: utils.py:18
def utils.functor (   code,
  kwds,
  debug = 0 
)
Auto-generate and execute function with given code and configuration
For details of compile/exec/eval see
http://lucumr.pocoo.org/2011/2/1/exec-in-python/

Definition at line 70 of file utils.py.

References join(), and print().

Referenced by pkg.AbstractPkg.write().

70 
71 def functor(code, kwds, debug=0):
72  """
73  Auto-generate and execute function with given code and configuration
74  For details of compile/exec/eval see
75  http://lucumr.pocoo.org/2011/2/1/exec-in-python/
76  """
77  args = []
78  for key, val in kwds.items():
79  if isinstance(val, str):
80  arg = '%s="%s"' % (key, val)
81  elif isinstance(val, list):
82  arg = '%s=%s' % (key, val)
83  else:
84  msg = 'Unsupported data type "%s" <%s>' % (val, type(val))
85  raise Exception(msg)
86  args.append(arg)
87  func = '\nimport sys'
88  func += '\nimport io'
89  func += "\ndef func(%s):\n" % ','.join(args)
90  func += code
91  func += """
92 def capture():
93  "Capture snippet printous"
94  old_stdout = sys.stdout
95  sys.stdout = io.StringIO()
96  func()
97  out = sys.stdout.getvalue()
98  sys.stdout = old_stdout
99  return out\n
100 capture()\n"""
101  if debug:
102  print("\n### generated code\n")
103  print(func)
104  # compile python code as exec statement
105  obj = compile(func, '<string>', 'exec')
106  # define execution namespace
107  namespace = {}
108  # execute compiled python code in given namespace
109  exec(obj, namespace)
110  # located generated function object, run it and return its results
111  return namespace['capture']()
def functor
Definition: utils.py:70
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
static std::string join(char **cmd)
Definition: RemoteFile.cc:19
def utils.get_relval_cmssw_version (   file)

Definition at line 544 of file utils.py.

545 def get_relval_cmssw_version(file):
546  cmssw_release = re.findall('(CMSSW_\d*_\d*_\d*(?:_[\w\d]*)?)-', file)
547  gr_r_version = re.findall('CMSSW_\d*_\d*_\d*(?:_[\w\d]*)?-([\w\d]*)_V\d*\w?(_[\w\d]*)?-v', file)
548  if cmssw_release and gr_r_version:
549  if "PU" in gr_r_version[0][0] and not "FastSim" in file:
550  __gt = re.sub('^[^_]*_', "", gr_r_version[0][0])
551  __process_string = gr_r_version[0][1]
552  return (__gt, __process_string)
553  elif "PU" in gr_r_version[0][0] and "FastSim" in file: #a check for FastSimPU samples
554  return (cmssw_release[0], "PU_") #with possibly different GT's
555  return (cmssw_release[0], gr_r_version[0])
def get_relval_cmssw_version
Definition: utils.py:544
def utils.get_relval_id (   file)
Returns unique relval ID (dataset name) for a given file.

Definition at line 556 of file utils.py.

References watchdog.group.

557 def get_relval_id(file):
558  """Returns unique relval ID (dataset name) for a given file."""
559  dataset_name = re.findall('R\d{9}__([\w\D]*)__CMSSW_', file)
560  __process_string = re.search('CMSSW_\d*_\d*_\d*(?:_[\w\d]*)?-([\w\d]*)_V\d*\w?(_[\w\d]*)?-v', file)
561  _ps = ""
562  if __process_string:
563  if "PU" in __process_string.group(1) and not "FastSim" in file:
564  _ps = re.search('^[^_]*_', __process_string.group(1)).group()
565  elif "PU" in __process_string.group(1) and "FastSim" in file:
566  return dataset_name[0]+"_", _ps ##some testing is needed
567  return dataset_name[0], _ps
tuple group
Definition: watchdog.py:82
def get_relval_id
Definition: utils.py:556
def utils.get_relval_max_version (   files)
Returns file with maximum version at a) beggining of the file,
e.g. DQM_V000M b) at the end of run, e.g. _run2012-vM. M has to be max.

Definition at line 532 of file utils.py.

References get_relval_version().

533 def get_relval_max_version(files):
534  """Returns file with maximum version at a) beggining of the file,
535  e.g. DQM_V000M b) at the end of run, e.g. _run2012-vM. M has to be max."""
536  max_file = files[0]
537  max_v = get_relval_version(files[0])
538  for file in files:
539  file_v = get_relval_version(file)
540  if file_v[1] > max_v[1] or ((file_v[1] == max_v[1]) and (file_v[0] > max_v[0])):
541  max_file = file
542  max_v = file_v
543  return max_file
def get_relval_version
-------------—— Make files pairs: RelVal utils ---------------——
Definition: utils.py:525
def get_relval_max_version
Definition: utils.py:532
def utils.get_relval_version (   file)

-------------—— Make files pairs: RelVal utils ---------------——

Returns tuple (CMSSW version, run version) for specified file.

Definition at line 525 of file utils.py.

Referenced by get_relval_max_version().

526 def get_relval_version(file):
527  """Returns tuple (CMSSW version, run version) for specified file."""
528  cmssw_version = re.findall('DQM_V(\d*)_', file)
529  run_version = re.findall('CMSSW_\d*_\d*_\d*(?:_[\w\d]*)?-[\w\d]*_V\d*\w?(?:_[\w\d]*)?-v(\d*)__', file)
530  if cmssw_version and run_version:
531  return (int(cmssw_version[0]), int(run_version[0]))
def get_relval_version
-------------—— Make files pairs: RelVal utils ---------------——
Definition: utils.py:525
def utils.get_relvaldata_cmssw_version (   file)
Returns tuple (CMSSW release, GR_R version) for specified RelValData file.

Definition at line 494 of file utils.py.

496  """Returns tuple (CMSSW release, GR_R version) for specified RelValData file."""
497  cmssw_release = re.findall('(CMSSW_\d*_\d*_\d*(?:_[\w\d]*)?)-', file)
498  gr_r_version = re.findall('-(GR_R_\d*_V\d*\w?)(?:_RelVal)?_', file)
499  if not gr_r_version:
500  gr_r_version = re.findall('CMSSW_\d*_\d*_\d*(?:_[\w\d]*)?-(\w*)_RelVal_', file)
501  if cmssw_release and gr_r_version:
502  return (cmssw_release[0], gr_r_version[0])
def get_relvaldata_cmssw_version
Definition: utils.py:494
def utils.get_relvaldata_id (   file)

-----------—— Make files pairs: RelValData utils --------------——

Returns unique relvaldata ID for a given file.

Definition at line 484 of file utils.py.

485 def get_relvaldata_id(file):
486  """Returns unique relvaldata ID for a given file."""
487  run_id = re.search('R\d{9}', file)
488  run = re.search('_RelVal_([\w\d]*)-v\d__', file)
489  if not run:
490  run = re.search('GR_R_\d*_V\d*C?_([\w\d]*)-v\d__', file)
491  if run_id and run:
492  return (run_id.group(), run.group(1))
493  return None
def get_relvaldata_id
-----------—— Make files pairs: RelValData utils --------------——
Definition: utils.py:484
def utils.get_relvaldata_max_version (   files)
Returns file with maximum version at a) beggining of the file,
e.g. DQM_V000M b) at the end of run, e.g. _run2012-vM. M has to be max.

Definition at line 512 of file utils.py.

References get_relvaldata_version().

513 def get_relvaldata_max_version(files):
514  """Returns file with maximum version at a) beggining of the file,
515  e.g. DQM_V000M b) at the end of run, e.g. _run2012-vM. M has to be max."""
516  max_file = files[0]
517  max_v = get_relvaldata_version(files[0])
518  for file in files:
519  file_v = get_relvaldata_version(file)
520  if file_v[1] > max_v[1] or ((file_v[1] == max_v[1]) and (file_v[0] > max_v[0])):
521  max_file = file
522  max_v = file_v
523  return max_file
def get_relvaldata_version
Definition: utils.py:503
def get_relvaldata_max_version
Definition: utils.py:512
def utils.get_relvaldata_version (   file)
Returns tuple (CMSSW version, run version) for specified file.

Definition at line 503 of file utils.py.

Referenced by get_relvaldata_max_version().

504 def get_relvaldata_version(file):
505  """Returns tuple (CMSSW version, run version) for specified file."""
506  cmssw_version = re.findall('DQM_V(\d*)_', file)
507  run_version = re.findall('_RelVal_[\w\d]*-v(\d)__', file)
508  if not run_version:
509  run_version = re.findall('GR_R_\d*_V\d*C?_[\w\d]*-v(\d)__', file)
510  if cmssw_version and run_version:
511  return (int(cmssw_version[0]), int(run_version[0]))
def get_relvaldata_version
Definition: utils.py:503
def utils.getNbins (   h)
To be used in loops on bin number with range()
For each dimension there are GetNbinsX()+2 bins including underflow 
and overflow, and range() loops starts from 0. So the total number
of bins as upper limit of a range() loop already includes the next 
to last value needed.

Definition at line 94 of file utils.py.

Referenced by utils.Chi2.absval(), utils.BinToBin.do_test(), utils.BinToBin1percent.do_test(), and utils.StatisticalTest.get_rank().

94 
95 def getNbins(h):
96  """
97  To be used in loops on bin number with range()
98  For each dimension there are GetNbinsX()+2 bins including underflow
99  and overflow, and range() loops starts from 0. So the total number
100  of bins as upper limit of a range() loop already includes the next
101  to last value needed.
102  """
103  biny=h.GetNbinsY()
104  if biny>1: biny+=2
105  binz=h.GetNbinsZ()
106  if binz>1:binz+=2
107  return (h.GetNbinsX()+2)*(biny)*(binz)
108 
109 #-------------------------------------------------------------------------------
110 
def getNbins
Definition: utils.py:94
def utils.is_empty (   h)

Definition at line 179 of file utils.py.

Referenced by DTSectColl.addTSTheta(), compileDMRTrends(), DEcompare< T >.get_ncand(), utils.StatisticalTest.get_rank(), runlistfromlumifile(), and DEcompare< T >.SortCollections().

180 def is_empty(h):
181  for i in range(0,getNbins(h)):
182  if h.GetBinContent(i)!=0: return False
183  return True
184  #return h.GetSumOfWeights()==0
185 
186 #-------------------------------------------------------------------------------
const uint16_t range(const Frame &aFrame)
def is_empty
Definition: utils.py:179
def getNbins
Definition: utils.py:94
def utils.is_relvaldata (   files)

----------------------— Make files pairs -----------------------—

Definition at line 569 of file utils.py.

References any().

Referenced by make_files_pairs().

570 def is_relvaldata(files):
571  is_relvaldata_re = re.compile('_RelVal_')
572  return any([is_relvaldata_re.search(filename) for filename in files])
bool any(const std::vector< T > &v, const T &what)
Definition: ECalSD.cc:37
def is_relvaldata
----------------------— Make files pairs -----------------------—
Definition: utils.py:569
def utils.is_sparse (   h)

Definition at line 187 of file utils.py.

References sistrip::SpyUtilities.range().

188 def is_sparse(h):
189  filled_bins=0.
190  nbins=h.GetNbinsX()
191  for ibin in range(0,nbins+2):
192  if h.GetBinContent(ibin)>0:
193  filled_bins+=1
194  #print "%s %s --> %s" %(filled_bins,nbins,filled_bins/nbins)
195  if filled_bins/nbins < .5:
196  return True
197  else:
198  return False
199 
200 #-------------------------------------------------------------------------------
def is_sparse
Definition: utils.py:187
const uint16_t range(const Frame &aFrame)
def utils.literal2root (   literal,
  rootType 
)

Definition at line 71 of file utils.py.

References print().

71 
72 def literal2root (literal,rootType):
73  bitsarray = array.array('B')
74  bitsarray.fromstring(literal.decode('hex'))
75 
76  tbuffer=0
77  try:
78  tbuffer = TBufferFile(TBufferFile.kRead, len(bitsarray), bitsarray, False,0)
79  except:
80  print("could not transform to object array:")
81  print([ i for i in bitsarray ])
82 
83  # replace a couple of shortcuts with the real root class name
84  if rootType == 'TPROF':
85  rootType = 'TProfile'
86  if rootType == 'TPROF2D':
87  rootType = 'TProfile2D'
88 
89  root_class=eval(rootType+'.Class()')
90 
91  return tbuffer.ReadObject(root_class)
92 
93 #-------------------------------------------------------------------------------
def literal2root
Definition: utils.py:71
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def utils.logger (   msg_level,
  message 
)

Definition at line 49 of file utils.py.

References print().

49 
50 def logger(msg_level,message):
51  if msg_level>=_log_level:
52  print("[%s] %s" %(asctime(),message))
53 
#-------------------------------------------------------------------------------
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def logger
Definition: utils.py:49
def utils.make_files_pairs (   files,
  verbose = True 
)

Definition at line 573 of file utils.py.

References bitset_utilities.append(), beamvalidation.exit(), is_relvaldata(), SiStripPI.max, print(), and str.

Referenced by ValidationMatrix.get_filenames_from_pool().

574 def make_files_pairs(files, verbose=True):
575  ## Select functions to use
576  if is_relvaldata(files):
577  is_relval_data = True
578  get_cmssw_version = get_relvaldata_cmssw_version
579  get_id = get_relvaldata_id
580  get_max_version = get_relvaldata_max_version
581  # print 'Pairing Data RelVal files.'
582  else:
583  is_relval_data = False
584  get_cmssw_version = get_relval_cmssw_version
585  get_id = get_relval_id
586  get_max_version = get_relval_max_version
587  # print 'Pairing Monte Carlo RelVal files.'
588 
589  ## Divide files into groups
590  versions_files = dict()
591  for file in files:
592  version = get_cmssw_version(file)
593  if version in versions_files:
594  versions_files[version].append(file)
595  else:
596  versions_files[version] = [file]
597 
598  ## Print the division into groups
599  if verbose:
600  print('\nFound versions:')
601  for version in versions_files:
602  print('%s: %d files' % (str(version), len(versions_files[version])))
603 
604  if len(versions_files) <= 1:
605  print('\nFound too little versions, there is nothing to pair. Exiting...\n')
606  exit()
607 
608  ## Select two biggest groups.
609  versions = versions_files.keys()
610  sizes = [len(value) for value in versions_files.values()]
611  v1 = versions[sizes.index(max(sizes))]
612  versions.remove(v1)
613  sizes.remove(max(sizes))
614  v2 = versions[sizes.index(max(sizes))]
615 
616  ## Print two biggest groups.
617  if verbose:
618  print('\nPairing %s (%d files) and %s (%d files)' % (str(v1),
619  len(versions_files[v1]), str(v2), len(versions_files[v2])))
620 
621  ## Pairing two versions
622  print('\nGot pairs:')
623  pairs = []
624  for unique_id in set([get_id(file) for file in versions_files[v1]]):
625  if is_relval_data:
626  dataset_re = re.compile(unique_id[0]+'_')
627  run_re = re.compile(unique_id[1])
628  c1_files = [file for file in versions_files[v1] if dataset_re.search(file) and run_re.search(file)]
629  c2_files = [file for file in versions_files[v2] if dataset_re.search(file) and run_re.search(file)]
630  else:
631  dataset_re = re.compile(unique_id[0]+'_')
632  ps_re = re.compile(unique_id[1])
633  ##compile a PU re and search also for same PU
634  c1_files = [file for file in versions_files[v1] if dataset_re.search(file) and ps_re.search(file)]
635  c2_files = [file for file in versions_files[v2] if dataset_re.search(file) and ps_re.search(file)]
636 
637  if len(c1_files) > 0 and len(c2_files) > 0:
638  first_file = get_max_version(c1_files)
639  second_file = get_max_version(c2_files)
640  print('%s\n%s\n' % (first_file, second_file))
641  pairs.extend((first_file, second_file))
642  if verbose:
643  print("Paired and got %d files.\n" % len(pairs))
644  return pairs
boost::dynamic_bitset append(const boost::dynamic_bitset<> &bs1, const boost::dynamic_bitset<> &bs2)
this method takes two bitsets bs1 and bs2 and returns result of bs2 appended to the end of bs1 ...
def is_relvaldata
----------------------— Make files pairs -----------------------—
Definition: utils.py:569
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def make_files_pairs
Definition: utils.py:573
#define str(s)
def utils.parse_word (   word)

Definition at line 37 of file utils.py.

References sistrip::SpyUtilities.range().

Referenced by pkg.AbstractPkg.tmpl_tags().

37 
38 def parse_word(word):
39  "Parse word which contas double underscore tag"
40  output = set()
41  words = word.split()
42  for idx in range(0, len(words)):
43  pat = words[idx]
44  if pat and len(pat) > 4 and pat[:2] == '__': # we found enclosure
45  tag = pat[2:pat.rfind('__')]
46  if tag.find('__') != -1: # another pattern
47  for item in tag.split('__'):
48  if TAG.match(item):
49  output.add('__%s__' % item)
50  else:
51  output.add('__%s__' % tag)
52  return output
def parse_word
Definition: utils.py:37
const uint16_t range(const Frame &aFrame)
def utils.profile2histo (   profile)

Definition at line 228 of file utils.py.

References sistrip::SpyUtilities.range().

229 def profile2histo(profile):
230  if not profile.InheritsFrom("TH1"):
231  return profile
232 
233  bin_low_edges=[]
234  n_bins=profile.GetNbinsX()
235 
236  for ibin in range(1,n_bins+2):
237  bin_low_edges.append(profile.GetBinLowEdge(ibin))
238  bin_low_edges=array.array('f',bin_low_edges)
239  histo=TH1F(profile.GetName(),profile.GetTitle(),n_bins,bin_low_edges)
240  for ibin in range(0,n_bins+2):
241  histo.SetBinContent(ibin,profile.GetBinContent(ibin))
242  histo.SetBinError(ibin,profile.GetBinError(ibin))
243 
244  return histo
245 #-------------------------------------------------------------------------------
const uint16_t range(const Frame &aFrame)
def profile2histo
Definition: utils.py:228
def utils.setTDRStyle ( )

Definition at line 54 of file utils.py.

References HILowLumiHLTOfflineSource_cfi.dirname.

54 
55 def setTDRStyle():
56  this_dir=dirname(this_module_name)
57  this_dir_one_up=this_dir[:this_dir.rfind("/")+1]
58  #this_dir_two_up=this_dir_one_up[:this_dir_one_up.rfind("/")+1]
59  style_file=''
60  if "RELMON_SA" in os.environ:
61  style_file=this_dir_one_up+"data/tdrstyle_mod.C"
62  else:
63  style_file="%s/src/Utilities/RelMon/data/tdrstyle_mod.C"%(os.environ["CMSSW_BASE"])
64  try:
65  gROOT.ProcessLine(".L %s" %style_file)
66  gROOT.ProcessLine("setTDRStyle()")
67  except:
68  "Print could not set the TDR style. File %s not found?" %style_file
69 
70 
#-------------------------------------------------------------------------------
def setTDRStyle
Definition: utils.py:54
def utils.template_directory ( )

Definition at line 23 of file utils.py.

Referenced by main.generator().

23 
24 def template_directory():
25  "Return location of template directory"
26  mkTemplates = "src/FWCore/Skeletons/mkTemplates"
27  # Check developer area first
28  if "CMSSW_BASE" in os.environ:
29  ret = os.path.join(os.environ["CMSSW_BASE"], mkTemplates)
30  if os.path.exists(ret):
31  return ret
32  # Then release area
33  ret = os.path.join(os.environ["CMSSW_RELEASE_BASE"], mkTemplates)
34  if not os.path.exists(ret):
35  raise Exception("Did not find 'FWCore/Skeletons/mkTemplates' directory in the developer area nor in the release area")
36  return ret
def template_directory
Definition: utils.py:23
def utils.test_env (   tdir,
  tmpl 
)
Test user environment, look-up if user has run cmsenv, otherwise
provide meaningful error message back to the user.

Definition at line 53 of file utils.py.

References print().

Referenced by main.generator().

53 
54 def test_env(tdir, tmpl):
55  """
56  Test user environment, look-up if user has run cmsenv, otherwise
57  provide meaningful error message back to the user.
58  """
59  if not tdir or not os.path.isdir(tdir):
60  print("Unable to access template dir: %s" % tdir)
61  sys.exit(1)
62  if not os.listdir(tdir):
63  print("No template files found in template dir %s" % tdir)
64  sys.exit(0)
65  if not tmpl:
66  msg = "No template type is provided, "
67  msg += "see available templates via --templates option"
68  print(msg)
69  sys.exit(1)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def test_env
Definition: utils.py:53
def utils.to_datetime (   date_string)
Takes a date string with the format Y-m-d H:m:S.f and gives back a datetime.datetime object

Definition at line 12 of file utils.py.

Referenced by querying_tests.global_tag_tests().

12 
13 def to_datetime(date_string):
14  """
15  Takes a date string with the format Y-m-d H:m:S.f and gives back a datetime.datetime object
16  """
17  return datetime.datetime.strptime(date_string.replace(",", "."), "%d-%b-%y %I:%M:%S.%f %p")
def to_datetime
Definition: utils.py:12
def utils.to_timestamp (   obj)
Takes a datetime object and outputs a timestamp string with the format Y-m-d H:m:S.f

Definition at line 6 of file utils.py.

6 
7 def to_timestamp(obj):
8  """
9  Takes a datetime object and outputs a timestamp string with the format Y-m-d H:m:S.f
10  """
11  return obj.strftime('%d-%b-%y %I:%M:%S.%f %p') if isinstance(obj, datetime.datetime) else obj
def to_timestamp
Definition: utils.py:6
def utils.tree (   idir)

Definition at line 143 of file utils.py.

References print().

144 def tree(idir):
145  "Print directory content, similar to tree UNIX command"
146  if idir[-1] == '/':
147  idir = idir[-1]
148  dsep = ''
149  fsep = ''
150  dtot = -1 # we'll not count initial directory
151  ftot = 0
152  for root, dirs, files in os.walk(idir):
153  dirs = root.split('/')
154  ndirs = len(dirs)
155  if ndirs > 1:
156  dsep = '| '*(ndirs-1)
157  print('%s%s/' % (dsep, dirs[-1]))
158  dtot += 1
159  for fname in files:
160  fsep = dsep + '|--'
161  print('%s %s' % (fsep, fname))
162  ftot += 1
163  if dtot == -1 or not dtot:
164  dmsg = ''
165  else:
166  dmsg = '%s directories,' % dtot
167  if ftot:
168  fmsg = '%s file' % ftot
169  if ftot > 1:
170  fmsg += 's'
171  else:
172  fmsg = ''
173  if dmsg and fmsg:
174  print("Total: %s %s" % (dmsg, fmsg))
175  else:
176  print("No directories/files in %s" % idir)
def tree
Definition: utils.py:143
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def utils.user_info (   ainput = None)

Definition at line 112 of file utils.py.

113 def user_info(ainput=None):
114  "Return user name and office location, based on UNIX finger"
115  if ainput:
116  return ainput
117  pwdstr = pwd.getpwnam(os.getlogin())
118  author = pwdstr.pw_gecos
119  if author and isinstance(author, str):
120  author = author.split(',')[0]
121  return author
def user_info
Definition: utils.py:112
def utils.wget (   url)
Fetch the WHOLE file, not in bunches... To be optimised.

Definition at line 459 of file utils.py.

References print(), and SiPixelLorentzAngle_cfi.read.

460 def wget(url):
461  """ Fetch the WHOLE file, not in bunches... To be optimised.
462  """
463  opener=build_opener(X509CertOpen())
464  datareq = Request(url)
465  datareq.add_header('authenticated_wget', "The ultimate wgetter")
466  bin_content=None
467  try:
468  filename=basename(url)
469  print("Checking existence of file %s on disk..."%filename)
470  if not isfile("./%s"%filename):
471  bin_content=opener.open(datareq).read()
472  else:
473  print("File %s exists, skipping.." %filename)
474  except ValueError:
475  print("Error: Unknown url %s" %url)
476 
477  if bin_content!=None:
478  ofile = open(filename, 'wb')
479  ofile.write(bin_content)
480  ofile.close()
481 
#-------------------------------------------------------------------------------
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def wget
Definition: utils.py:459

Variable Documentation

int utils._log_level = 10

Definition at line 48 of file utils.py.

tuple utils.TAG = re.compile(r'[a-zA-Z0-9]')

Definition at line 21 of file utils.py.

utils.theargv = sys.argv

Definition at line 24 of file utils.py.