Skip to content

Commit

Permalink
ignore branches in set buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
Noel Dawe authored and Noel Dawe committed Apr 17, 2012
1 parent 4874f43 commit caf7e1a
Showing 1 changed file with 30 additions and 22 deletions.
52 changes: 30 additions & 22 deletions rootpy/tree/tree.py
Expand Up @@ -300,16 +300,23 @@ def update_buffer(self, buffer, transfer_objects=False):


def set_buffer(self, buffer, def set_buffer(self, buffer,
variables=None, variables=None,
ignore_variables=None,
create_branches=False, create_branches=False,
visible=True, visible=True,
ignore_missing=False, ignore_missing=False,
transfer_objects=False): transfer_objects=False):


# determine variables to keep
all_variables = buffer.keys()
if variables is None:
variables = all_variables
if ignore_variables is None:
ignore_variables = []
variables = (set(all_variables) & set(variables)) - set(ignore_variables)

if create_branches: if create_branches:
for name, value in buffer.items(): for name in variables:
if variables is not None: value = buffer[name]
if name not in variables:
continue
if self.has_branch(name): if self.has_branch(name):
raise ValueError( raise ValueError(
"Attempting to create two branches " "Attempting to create two branches "
Expand All @@ -319,23 +326,20 @@ def set_buffer(self, buffer,
else: else:
self.Branch(name, value) self.Branch(name, value)
else: else:
for name, value in buffer.items(): for name in variables:
if variables is not None: value = buffer[name]
if name not in variables:
continue
if self.has_branch(name): if self.has_branch(name):
self.SetBranchAddress(name, value) self.SetBranchAddress(name, value)
elif not ignore_missing: elif not ignore_missing:
raise ValueError( raise ValueError(
"Attempting to set address for " "Attempting to set address for "
"branch %s which does not exist" % name) "branch %s which does not exist" % name)
if visible: if visible:
if variables: newbuffer = TreeBuffer()
newbuffer = TreeBuffer() for variable in variables:
for variable in variables: if variable in buffer:
if variable in buffer: newbuffer[variable] = buffer[variable]
newbuffer[variable] = buffer[variable] buffer = newbuffer
buffer = newbuffer
self.update_buffer(buffer, transfer_objects=transfer_objects) self.update_buffer(buffer, transfer_objects=transfer_objects)


def activate(self, variables, exclusive=False): def activate(self, variables, exclusive=False):
Expand Down Expand Up @@ -395,19 +399,23 @@ def iterbranchnames(self):
for branch in self.iterbranches(): for branch in self.iterbranches():
yield branch.GetName() yield branch.GetName()


def glob(self, pattern, prune=None): def glob(self, patterns, prune=None):
""" """
Return a list of branch names that match pattern. Return a list of branch names that match pattern.
Exclude all matched branch names which also match a pattern in prune. Exclude all matched branch names which also match a pattern in prune.
prune may be a string or list of strings. prune may be a string or list of strings.
""" """
matches = fnmatch.filter(self.iterbranchnames(), pattern) if isinstance(patterns, basestring):
if prune is not None: patterns = [patterns]
if isinstance(prune, basestring): if isinstance(prune, basestring):
prune = [prune] prune = [prune]
for prune_pattern in prune: matches = []
matches = [match for match in matches for pattern in patterns:
if not fnmatch.fnmatch(match, prune_pattern)] matches += fnmatch.filter(self.iterbranchnames(), pattern)
if prune is not None:
for prune_pattern in prune:
matches = [match for match in matches
if not fnmatch.fnmatch(match, prune_pattern)]
return matches return matches


def __contains__(self, branch): def __contains__(self, branch):
Expand Down

0 comments on commit caf7e1a

Please sign in to comment.