Skip to main content
added 202 characters in body
Source Link
Martijn Pieters
  • 1.1m
  • 325
  • 4.2k
  • 3.4k

Namespaces are just pythonPython objects, and you can assign objects (including the result of attribute lookups) to local variable names:

strands = translation.strands
active = translation.active
locus = translation.locus

Alternatively, you'd have to hack together a context manager that modifies locals(), as shown in this answer: https://stackoverflow.com/a/12486075/100297this answer.

Something like this would do that:

import inspect

class Namespace(object):
    def __init__(self, namespaced):
        self.namespaced = namespaced

    def __enter__(self):
        """store the pre-contextmanager scope"""
        ns = globals()
        namespaced = self.namespaced.__dict__
        # keep track of what we add and what we replace
        self.scope_added = namespaced.keys()
        self.scope_before = {k: v for k, v in ns.iteritemsitems() if k in self.scope_added}
        globals().update(namespaced)
        return self

    def __exit__(self):
        ns = globals()
        # remove what we added, then reinstate what we replaced
        for name in self.scope_added:
            if name in ns:
                del ns[name]
        ns.update(self.scope_before)

then use it like this:

with Namespace(translation):
     del strands[active][locus]

where all items in the translation.__dict__ are made available globally while in the while block.

Note that this is not thread-safe, and has the potential to create lots of confusion for anyone trying to read code that uses this in future, including yourself. Personally, I wouldn't use this.

Namespaces are just python objects, and you can assign objects (including the result of attribute lookups) to local variable names:

strands = translation.strands
active = translation.active
locus = translation.locus

Alternatively, you'd have to hack together a context manager that modifies locals(), as shown in this answer: https://stackoverflow.com/a/12486075/100297

Something like this would do that:

import inspect

class Namespace(object):
    def __init__(self, namespaced):
        self.namespaced = namespaced

    def __enter__(self):
        """store the pre-contextmanager scope"""
        ns = globals()
        namespaced = self.namespaced.__dict__
        # keep track of what we add and what we replace
        self.scope_added = namespaced.keys()
        self.scope_before = {k: v for k, v in ns.iteritems() if k in self.scope_added}
        globals().update(namespaced)
        return self

    def __exit__(self:
        ns = globals()
        # remove what we added, then reinstate what we replaced
        for name in self.scope_added:
            if name in ns:
                del ns[name]
        ns.update(self.scope_before)

then use it like this:

with Namespace(translation):
     del strands[active][locus]

where all items in the translation.__dict__ are made available globally while in the while block.

Namespaces are just Python objects, and you can assign objects (including the result of attribute lookups) to local variable names:

strands = translation.strands
active = translation.active
locus = translation.locus

Alternatively, you'd have to hack together a context manager that modifies locals(), as shown in this answer.

Something like this would do that:

import inspect

class Namespace(object):
    def __init__(self, namespaced):
        self.namespaced = namespaced

    def __enter__(self):
        """store the pre-contextmanager scope"""
        ns = globals()
        namespaced = self.namespaced.__dict__
        # keep track of what we add and what we replace
        self.scope_added = namespaced.keys()
        self.scope_before = {k: v for k, v in ns.items() if k in self.scope_added}
        globals().update(namespaced)
        return self

    def __exit__(self):
        ns = globals()
        # remove what we added, then reinstate what we replaced
        for name in self.scope_added:
            if name in ns:
                del ns[name]
        ns.update(self.scope_before)

then use it like this:

with Namespace(translation):
     del strands[active][locus]

where all items in the translation.__dict__ are made available globally while in the while block.

Note that this is not thread-safe, and has the potential to create lots of confusion for anyone trying to read code that uses this in future, including yourself. Personally, I wouldn't use this.

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

Namespaces are just python objects, and you can assign objects (including the result of attribute lookups) to local variable names:

strands = translation.strands
active = translation.active
locus = translation.locus

Alternatively, you'd have to hack together a context manager that modifies locals(), as shown in this answer: http://stackoverflow.com/a/12486075/100297https://stackoverflow.com/a/12486075/100297

Something like this would do that:

import inspect

class Namespace(object):
    def __init__(self, namespaced):
        self.namespaced = namespaced

    def __enter__(self):
        """store the pre-contextmanager scope"""
        ns = globals()
        namespaced = self.namespaced.__dict__
        # keep track of what we add and what we replace
        self.scope_added = namespaced.keys()
        self.scope_before = {k: v for k, v in ns.iteritems() if k in self.scope_added}
        globals().update(namespaced)
        return self

    def __exit__(self:
        ns = globals()
        # remove what we added, then reinstate what we replaced
        for name in self.scope_added:
            if name in ns:
                del ns[name]
        ns.update(self.scope_before)

then use it like this:

with Namespace(translation):
     del strands[active][locus]

where all items in the translation.__dict__ are made available globally while in the while block.

Namespaces are just python objects, and you can assign objects (including the result of attribute lookups) to local variable names:

strands = translation.strands
active = translation.active
locus = translation.locus

Alternatively, you'd have to hack together a context manager that modifies locals(), as shown in this answer: http://stackoverflow.com/a/12486075/100297

Something like this would do that:

import inspect

class Namespace(object):
    def __init__(self, namespaced):
        self.namespaced = namespaced

    def __enter__(self):
        """store the pre-contextmanager scope"""
        ns = globals()
        namespaced = self.namespaced.__dict__
        # keep track of what we add and what we replace
        self.scope_added = namespaced.keys()
        self.scope_before = {k: v for k, v in ns.iteritems() if k in self.scope_added}
        globals().update(namespaced)
        return self

    def __exit__(self:
        ns = globals()
        # remove what we added, then reinstate what we replaced
        for name in self.scope_added:
            if name in ns:
                del ns[name]
        ns.update(self.scope_before)

then use it like this:

with Namespace(translation):
     del strands[active][locus]

where all items in the translation.__dict__ are made available globally while in the while block.

Namespaces are just python objects, and you can assign objects (including the result of attribute lookups) to local variable names:

strands = translation.strands
active = translation.active
locus = translation.locus

Alternatively, you'd have to hack together a context manager that modifies locals(), as shown in this answer: https://stackoverflow.com/a/12486075/100297

Something like this would do that:

import inspect

class Namespace(object):
    def __init__(self, namespaced):
        self.namespaced = namespaced

    def __enter__(self):
        """store the pre-contextmanager scope"""
        ns = globals()
        namespaced = self.namespaced.__dict__
        # keep track of what we add and what we replace
        self.scope_added = namespaced.keys()
        self.scope_before = {k: v for k, v in ns.iteritems() if k in self.scope_added}
        globals().update(namespaced)
        return self

    def __exit__(self:
        ns = globals()
        # remove what we added, then reinstate what we replaced
        for name in self.scope_added:
            if name in ns:
                del ns[name]
        ns.update(self.scope_before)

then use it like this:

with Namespace(translation):
     del strands[active][locus]

where all items in the translation.__dict__ are made available globally while in the while block.

added 130 characters in body
Source Link
Martijn Pieters
  • 1.1m
  • 325
  • 4.2k
  • 3.4k

Namespaces are just python objects, and you can assign objects (including the result of attribute lookups) to local variable names:

strands = translation.strands
active = translation.active
locus = translation.locus

Alternatively, you'd have to hack together a context manager that modifies locals(), as shown in this answer: http://stackoverflow.com/a/12486075/100297

Something like this would do that:

import inspect

class Namespace(object):
    def __init__(self, namespaced):
        self.namespaced = namespaced

    def __enter__(self):
        """store the pre-contextmanager scope"""
        ns = globals()
        namespaced = self.namespaced.__dict__
        # keep track of what we add and what we replace
        self.scope_added = namespaced.keys()
        self.scope_before = {k: v for k, v in ns.iteritems() if k in self.scope_added}
        globals().update(namespaced)
        return self

    def __exit__(self:
        ns = globals()
        # remove what we added, then reinstate what we replaced
        for name in self.scope_added:
            if name in ns:
                del ns[name]
        ns.update(self.scope_before)

then use it like this:

with Namespace(translation):
     del strands[active][locus]

where all items in the translation.__dict__ are made available globally while in the while block.

Namespaces are just python objects, and you can assign objects (including the result of attribute lookups) to local variable names:

strands = translation.strands
active = translation.active
locus = translation.locus

Alternatively, you'd have to hack together a context manager that modifies locals(), as shown in this answer: http://stackoverflow.com/a/12486075/100297

Something like this would do that:

import inspect

class Namespace(object):
    def __init__(self, namespaced):
        self.namespaced = namespaced

    def __enter__(self):
        """store the pre-contextmanager scope"""
        ns = globals()
        namespaced = self.namespaced.__dict__
        self.scope_added = namespaced.keys()
        self.scope_before = {k: v for k, v in ns.iteritems() if k in self.scope_added}
        globals().update(namespaced)
        return self

    def __exit__(self:
        ns = globals()
        for name in self.scope_added:
            if name in ns:
                del ns[name]
        ns.update(self.scope_before)

then use it like this:

with Namespace(translation):
     del strands[active][locus]

where all items in the translation.__dict__ are made available globally while in the while block.

Namespaces are just python objects, and you can assign objects (including the result of attribute lookups) to local variable names:

strands = translation.strands
active = translation.active
locus = translation.locus

Alternatively, you'd have to hack together a context manager that modifies locals(), as shown in this answer: http://stackoverflow.com/a/12486075/100297

Something like this would do that:

import inspect

class Namespace(object):
    def __init__(self, namespaced):
        self.namespaced = namespaced

    def __enter__(self):
        """store the pre-contextmanager scope"""
        ns = globals()
        namespaced = self.namespaced.__dict__
        # keep track of what we add and what we replace
        self.scope_added = namespaced.keys()
        self.scope_before = {k: v for k, v in ns.iteritems() if k in self.scope_added}
        globals().update(namespaced)
        return self

    def __exit__(self:
        ns = globals()
        # remove what we added, then reinstate what we replaced
        for name in self.scope_added:
            if name in ns:
                del ns[name]
        ns.update(self.scope_before)

then use it like this:

with Namespace(translation):
     del strands[active][locus]

where all items in the translation.__dict__ are made available globally while in the while block.

added 79 characters in body
Source Link
Martijn Pieters
  • 1.1m
  • 325
  • 4.2k
  • 3.4k
Loading
added 11 characters in body
Source Link
Martijn Pieters
  • 1.1m
  • 325
  • 4.2k
  • 3.4k
Loading
added 158 characters in body
Source Link
Martijn Pieters
  • 1.1m
  • 325
  • 4.2k
  • 3.4k
Loading
added 158 characters in body
Source Link
Martijn Pieters
  • 1.1m
  • 325
  • 4.2k
  • 3.4k
Loading
Source Link
Martijn Pieters
  • 1.1m
  • 325
  • 4.2k
  • 3.4k
Loading