Ejemplo n.º 1
0
def localstack_demo():
    ls = LocalStack()
    a = ls.push(100)
    # a.pop()
    b = ls.push(120)
    print(a, b)
    print(ls.pop())
    print(ls.pop())
Ejemplo n.º 2
0
def test1():

    stack = LocalStack()

    stack.push(1)
    stack.push(2)
    stack.push(3)
    stack.push(4)
    stack.push(5)

    print(stack.pop())
    print(stack.pop())
    print(stack.pop())
    print(stack.pop())
    print(stack.pop())
    print(stack.pop())
    print(stack.pop())
Ejemplo n.º 3
0
def test_local_stack():
    """Test the LocalStack"""
    ident = get_ident()

    ls = LocalStack()
    assert ident not in ls._local.__storage__
    assert ls.top is None
    ls.push(42)
    assert ident in ls._local.__storage__
    assert ls.top == 42
    ls.push(23)
    assert ls.top == 23
    ls.pop()
    assert ls.top == 42
    ls.pop()
    assert ls.top is None
    assert ls.pop() is None
    assert ls.pop() is None

    proxy = ls()
    ls.push([1, 2])
    assert proxy == [1, 2]
    ls.push((1, 2))
    assert proxy == (1, 2)
    ls.pop()
    ls.pop()
    assert repr(proxy) == "<LocalProxy unbound>"

    assert ident not in ls._local.__storage__
Ejemplo n.º 4
0
def test_custom_idents():
    """Local manager supports custom ident functions"""
    ident = 0
    local = Local()
    stack = LocalStack()
    mgr = LocalManager([local, stack], ident_func=lambda: ident)

    local.foo = 42
    stack.push({"foo": 42})
    ident = 1
    local.foo = 23
    stack.push({"foo": 23})
    ident = 0
    assert local.foo == 42
    assert stack.top["foo"] == 42
    stack.pop()
    assert stack.top is None
    ident = 1
    assert local.foo == 23
    assert stack.top["foo"] == 23
    stack.pop()
    assert stack.top is None
Ejemplo n.º 5
0
from werkzeug.local import LocalStack

l = LocalStack()
l.push(42)
l.push(23)

l.pop()
Ejemplo n.º 6
0
# @Time : 2020/2/20 15:11
# @Author : tongyue
from werkzeug.local import LocalStack

s = LocalStack()

s.push(1)
s.push(2)
print(s.top)  #取栈顶元素而已, top没有()是加了@property
print(s.pop())  #弹出栈顶元素
print(s.top)  #取栈顶元素而已
Ejemplo n.º 7
0
from werkzeug.local import LocalStack

s = LocalStack()
s.push(1)
print(s.top)
print(s.top)
print(s.pop())
print(s.top)
Ejemplo n.º 8
0
    new_t.start()

    线程隔离的是变量的值
    storage{'ident':{'name':'value'}}

6-10 线程隔离的栈:LocalStack
    

6-11 LocalStack的基本用法
    from werkzeug.local import LocalStack
    引入实例化
    s = LocalStack()
    推入栈
    s.push(1)
    取出栈
    s.pop(1)
    调用栈顶
    s.top

6-12 LocalStack作为线程隔离对象的意义
    使当前线程能够正确到引用到他自己所创建的对象,而不是引用到其他线程所创建的对象。
    对象是保存变量状态的地方

# 2018-12-03 16:28:22
6-13 flask中被线程隔离的对象
    连续发起两次请求,开启前需要打开多线程threading=True
    @web.route('/test')
def test():
    from .nolocal import n

    print('引入自定义的非线程隔离的变量原始值V:%s' % n.v)
from werkzeug.local import Local, LocalStack, LocalProxy

ls = LocalStack()
ls.push(42)
print(ls.top)
ls.push(23)
print(ls.top)
print(ls.pop())
print(ls.top)
Ejemplo n.º 10
0
"""
@author: MH
@contact: [email protected]
@file: test2.py
@time: 2018/7/24 10:31

        LocalStack<线程隔离栈>
"""
from werkzeug.local import LocalStack
# 实例化县城隔离栈
s = LocalStack()

# 将想要隔离的对象推入到栈中
s.push('dm')

# 取栈顶元素
print(s.top)

# top方法并不会清除掉栈内元素
print(s.top)

# 出栈,释放资源(释放栈顶元素)
s.pop()

# 出栈以后栈内资源被释放,所以栈顶元素为空
print(s.top)
Ejemplo n.º 11
0
# _*_ coding:utf-8 _*_
__author__ = 'Xbc'

from werkzeug.local import LocalStack

s = LocalStack()
# push  pop  top

s.push(1)  # 添加
print(s.top)  # 取
print(s.top)  # 取
print(s.pop())  # 删除
print(s.top)  # 取

# 1
# 1
# 1
# None

s.push(1)
print(s.top)
s.push(2)
# 栈 后进先出
print(s.top)
print(s.top)
print(s.pop())
print(s.top)

# s[7] 栈  序列(列表)
# 数据结构  限制了某些能力
Ejemplo n.º 12
0
class LoggingMiddleware(object):

    def __init__(self, app, auto_install=True):
        self.app = app
        self.context = LocalStack()
        if auto_install:
            logger = getLogger()
            logger.setLevel(DEBUG)
            logger.addHandler(self.handler)

    @cached_property
    def handler(self):
        return WSGIHandler(self)

    def __call__(self, environ, start_response):
        html = [False]
        def start(status, headers, exc_info=None):
            headers = list(headers)
            replaced_headers = []
            for k, v in headers:
                name = k.lower().strip()
                if name == 'content-type':
                    content_type = v.lower().split(';')[0]
                    content_type = content_type.strip().lower()
                    html[0] = content_type == 'text/html'
                if name != 'content-length':
                    replaced_headers.append((k, v))
            return start_response(status, replaced_headers)
        self.context.push([])
        result = self.app(environ, start)
        result = list(result)
        if html[0]:
            return self.inject_log_html(result)
        return result

    def inject_log_html(self, iterable):
        injected = False
        for chunk in iterable:
            if not injected and '</body>' in chunk:
                a, b = chunk.split('</body>', 1)
                yield a
                for subchunk in self.log_html():
                    yield subchunk
                yield b
                injected = True
            else:
                yield chunk
        if not injected:
            for chunk in self.log_html():
                yield chunk

    def log_html(self):
        level_map = {DEBUG: 'debug', INFO: 'info', WARNING: 'warn',
                     ERROR: 'error', CRITICAL: 'error'}
        records = self.context.pop()
        yield '<script>\n//<![CDATA[\nif (console) {\n'
        for record in records:
            yield 'console.'
            yield level_map[record.levelno]
            yield '('
            yield dumps(record.name)
            yield ' + ": " + '
            yield dumps(str(record.getMessage()))
            yield ');'
        yield '\n}\n// ]]>\n</script>'
Ejemplo n.º 13
0
from werkzeug.local import LocalStack

# 要创建一个栈对象, 要实现push, pop, top方法
# 1. 实例化LocalStack

s = LocalStack()
s.push(1)  # 压栈
print(s.top)  # 取栈顶, top不是方法是属性, 调用的时候不需要括号
print(s.top)  # 栈顶元素不会被删除
print(s.pop())  # 弹出栈顶元素
print(s.top)  # 元素已经被删除

s.push(1)
s.push(2)
print(s.top)
print(s.top)  # 两次取的都是同一个元素
s.pop()  # 后进先出
print(s.top)
Ejemplo n.º 14
0
# coding:utf-8

import threading
from werkzeug.local import Local, LocalStack

st = LocalStack()

for item in range(10):
    st.push(item)

for item in range(10):
    print(st.top)
    print(st.pop())