<commit_before># -*- coding: utf-8 -*-
"""
Display system RAM and CPU utilization.

Configuration parameters:
    format: output format string
    high_threshold: percent to consider CPU or RAM usage as 'high load'
    med_threshold: percent to consider CPU or RAM usage as 'medium load'

Format of status string placeholders:
    {cpu_temp} cpu temperature
    {cpu_usage} cpu usage percentage
    {mem_total} total memory
    {mem_used} used memory
    {mem_used_percent} used memory percentage

NOTE: If using the `{cpu_temp}` option, the `sensors` command should
be available, provided by the `lm-sensors` or `lm_sensors` package.

@author Shahin Azad <ishahinism at Gmail>, shrimpza
"""

import re
import subprocess
from time import time

class GetData:
    """
    Get system status
    """

    def execCMD(self, cmd, arg):
        """
        Take a system command and its argument, then return the result.

        Arguments:
        - `cmd`: system command.
        - `arg`: argument.
        """
        result = subprocess.check_output([cmd, arg])
        result = result.decode('utf-8')
        return result

    def cpu(self):
        """
        Get the cpu usage data from /proc/stat :
          cpu  2255 34 2290 22625563 6290 127 456 0 0
          - user: normal processes executing in user mode
          - nice: niced processes executing in user mode
          - system: processes executing in kernel mode
          - idle: twiddling thumbs
          - iowait: waiting for I/O to complete
          - irq: servicing interrupts
          - softirq: servicing softirqs
          - steal: involuntary wait
          - guest: running a normal guest
          - guest_nice: running a niced guest
        These numbers identify the amount of time the CPU has spent performing
        different kinds of work.  Time units are in USER_HZ
        (typically hundredths of a second)
        """
        with open('/proc/stat', 'r') as fd:
            line = fd.readline()
        cpu_data = line.split()
        total_cpu_time = sum(map(int, cpu_data[1:]))
        cpu_idle_time = int(cpu_data[4])

        # return the cpu total&idle time
        return total_cpu_time, cpu_idle_time

    def memory(self):
        """
        Execute 'free -m' command, grab the memory capacity and used size
        then return; Memory size 'total_mem', Used_mem, and percentage
        of used memory.
        """
        # Run 'free -m' command and make a list from output.
        mem_data = self.execCMD('free', '-m').split()
        mem_index = mem_data.index('Mem:')
        total_mem = int(mem_data[mem_index + 1]) / 1024.
        used_mem = int(mem_data[mem_index + 2]) / 1024.

        # Caculate percentage
        used_mem_percent = int(used_mem / (total_mem / 100))

        # Results are in kilobyte.
        return total_mem, used_mem, used_mem_percent

    def cpuTemp(self):
        """
        Tries to determine CPU temperature using the 'sensors' command.
        Searches for the CPU temperature by looking for a value prefixed
        by either "CPU Temp" or "Core 0" - does not look for or average
        out temperatures of all codes if more than one.
        """

        sensors = subprocess.check_output('sensors', shell=True)
        m = re.search("(Core 0|CPU Temp).+\+(.+).+\(.+", sensors)
        if m:
            cpu_temp = m.groups()[1].strip()
        else:
            cpu_temp = 'Unknown'

        return cpu_temp

class Py3status:
    """
    """
    # available configuration parameters
    cache_timeout = 10
    format = "CPU: {cpu_usage}%, " \
        "Mem: {mem_used}/{mem_total} GB ({mem_used_percent}%)"
    high_threshold = 75
    med_threshold = 40

    def __init__(self):
        self.data = GetData()
        self.cpu_total = 0
        self.cpu_idle = 0

    def sysData(self, i3s_output_list, i3s_config):
        # get CPU usage info
        cpu_total, cpu_idle = self.data.cpu()
        cpu_usage = 1 - (
            float(cpu_idle-self.cpu_idle) / float(cpu_total-self.cpu_total)
            )
        self.cpu_total = cpu_total
        self.cpu_idle = cpu_idle

        # if specified as a formatting option, also get the CPU temperature
        if '{cpu_temp}' in self.format:
            cpu_temp = self.data.cpuTemp()
        else:
            cpu_temp = ''

        # get RAM usage info
        mem_total, mem_used, mem_used_percent = self.data.memory()

        response = {
            'cached_until': time() + self.cache_timeout,
            'full_text': self.format.format(
                cpu_usage='%.2f' % (cpu_usage * 100),
                cpu_temp=cpu_temp,
                mem_used='%.2f' % mem_used,
                mem_total='%.2f' % mem_total,
                mem_used_percent='%.2f' % mem_used_percent,
            )
        }

        if max(cpu_usage, mem_used_percent/100) <= self.med_threshold / 100.0:
            response['color'] = i3s_config['color_good']
        elif max(cpu_usage, mem_used_percent/100) <= self.high_threshold / 100.0:
            response['color'] = i3s_config['color_degraded']
        else:
            response['color'] = i3s_config['color_bad']

        return response

if __name__ == "__main__":
    """
    Test this module by calling it directly.
    """
    from time import sleep
    x = Py3status()
    config = {
        'color_bad': '#FF0000',
        'color_degraded': '#FFFF00',
        'color_good': '#00FF00',
    }

    while True:
        print(x.sysData([], config))
        sleep(1)
<commit_msg>Update sysdata for python3 compatibility<commit_after># -*- coding: utf-8 -*-
"""
Display system RAM and CPU utilization.

Configuration parameters:
    format: output format string
    high_threshold: percent to consider CPU or RAM usage as 'high load'
    med_threshold: percent to consider CPU or RAM usage as 'medium load'

Format of status string placeholders:
    {cpu_temp} cpu temperature
    {cpu_usage} cpu usage percentage
    {mem_total} total memory
    {mem_used} used memory
    {mem_used_percent} used memory percentage

NOTE: If using the `{cpu_temp}` option, the `sensors` command should
be available, provided by the `lm-sensors` or `lm_sensors` package.

@author Shahin Azad <ishahinism at Gmail>, shrimpza
"""

import re
import subprocess
from time import time

class GetData:
    """
    Get system status
    """

    def execCMD(self, cmd, arg):
        """
        Take a system command and its argument, then return the result.

        Arguments:
        - `cmd`: system command.
        - `arg`: argument.
        """
        result = subprocess.check_output([cmd, arg])
        result = result.decode('utf-8')
        return result

    def cpu(self):
        """
        Get the cpu usage data from /proc/stat :
          cpu  2255 34 2290 22625563 6290 127 456 0 0
          - user: normal processes executing in user mode
          - nice: niced processes executing in user mode
          - system: processes executing in kernel mode
          - idle: twiddling thumbs
          - iowait: waiting for I/O to complete
          - irq: servicing interrupts
          - softirq: servicing softirqs
          - steal: involuntary wait
          - guest: running a normal guest
          - guest_nice: running a niced guest
        These numbers identify the amount of time the CPU has spent performing
        different kinds of work.  Time units are in USER_HZ
        (typically hundredths of a second)
        """
        with open('/proc/stat', 'r') as fd:
            line = fd.readline()
        cpu_data = line.split()
        total_cpu_time = sum(map(int, cpu_data[1:]))
        cpu_idle_time = int(cpu_data[4])

        # return the cpu total&idle time
        return total_cpu_time, cpu_idle_time

    def memory(self):
        """
        Execute 'free -m' command, grab the memory capacity and used size
        then return; Memory size 'total_mem', Used_mem, and percentage
        of used memory.
        """
        # Run 'free -m' command and make a list from output.
        mem_data = self.execCMD('free', '-m').split()
        mem_index = mem_data.index('Mem:')
        total_mem = int(mem_data[mem_index + 1]) / 1024.
        used_mem = int(mem_data[mem_index + 2]) / 1024.

        # Caculate percentage
        used_mem_percent = int(used_mem / (total_mem / 100))

        # Results are in kilobyte.
        return total_mem, used_mem, used_mem_percent

    def cpuTemp(self):
        """
        Tries to determine CPU temperature using the 'sensors' command.
        Searches for the CPU temperature by looking for a value prefixed
        by either "CPU Temp" or "Core 0" - does not look for or average
        out temperatures of all codes if more than one.
        """

        sensors = subprocess.check_output(
            'sensors',
            shell=True,
            universal_newlines=True,
        )
        m = re.search("(Core 0|CPU Temp).+\+(.+).+\(.+", sensors)
        if m:
            cpu_temp = m.groups()[1].strip()
        else:
            cpu_temp = 'Unknown'

        return cpu_temp

class Py3status:
    """
    """
    # available configuration parameters
    cache_timeout = 10
    format = "CPU: {cpu_usage}%, " \
        "Mem: {mem_used}/{mem_total} GB ({mem_used_percent}%)"
    high_threshold = 75
    med_threshold = 40

    def __init__(self):
        self.data = GetData()
        self.cpu_total = 0
        self.cpu_idle = 0

    def sysData(self, i3s_output_list, i3s_config):
        # get CPU usage info
        cpu_total, cpu_idle = self.data.cpu()
        cpu_usage = 1 - (
            float(cpu_idle-self.cpu_idle) / float(cpu_total-self.cpu_total)
            )
        self.cpu_total = cpu_total
        self.cpu_idle = cpu_idle

        # if specified as a formatting option, also get the CPU temperature
        if '{cpu_temp}' in self.format:
            cpu_temp = self.data.cpuTemp()
        else:
            cpu_temp = ''

        # get RAM usage info
        mem_total, mem_used, mem_used_percent = self.data.memory()

        response = {
            'cached_until': time() + self.cache_timeout,
            'full_text': self.format.format(
                cpu_usage='%.2f' % (cpu_usage * 100),
                cpu_temp=cpu_temp,
                mem_used='%.2f' % mem_used,
                mem_total='%.2f' % mem_total,
                mem_used_percent='%.2f' % mem_used_percent,
            )
        }

        if max(cpu_usage, mem_used_percent/100) <= self.med_threshold / 100.0:
            response['color'] = i3s_config['color_good']
        elif max(cpu_usage, mem_used_percent/100) <= self.high_threshold / 100.0:
            response['color'] = i3s_config['color_degraded']
        else:
            response['color'] = i3s_config['color_bad']

        return response

if __name__ == "__main__":
    """
    Test this module by calling it directly.
    """
    from time import sleep
    x = Py3status()
    config = {
        'color_bad': '#FF0000',
        'color_degraded': '#FFFF00',
        'color_good': '#00FF00',
    }

    while True:
        print(x.sysData([], config))
        sleep(1)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from tag_base import TagBase

class TagPLP(TagBase):

    def __init__(self, cartao_postagem, id_lpl=0, valor_global=0.00,
                 mcu_unidade_postagem='', nome_unidade_postagem=''):
        self.id_lpl = id_lpl
        self.valor_global = valor_global
        self.mcu_unidade_postagem = mcu_unidade_postagem
        self.nome_unidade_postagem = nome_unidade_postagem
        self.cartao_postagem = cartao_postagem

    def xml(self):
        xml = u'<plp>'
        xml += u'<id_plp>%s</id_plp>' % str(self.id_lpl) if self.id_lpl else ''
        xml += u'<valor_global>%s</valor_global>' % str(self.valor_global) if\
            self.valor_global else ''
        xml += u'<mcu_unidade_postagem>%s</mcu_unidade_postagem>' % \
               self.mcu_unidade_postagem
        xml += u'<nome_unidade_postagem>%s</nome_unidade_postagem>' %  \
               self.nome_unidade_postagem
        xml += u'<cartao_postagem>%s</cartao_postagem>' % self.cartao_postagem
        xml += u'</plp>'
        return xml
<commit_msg>xml de tag_plp validado<commit_after># -*- coding: utf-8 -*-
from tag_base import TagBase

class TagPLP(TagBase):

    def __init__(self, cartao_postagem, id_lpl=False, valor_global=0.00,
                 mcu_unidade_postagem='', nome_unidade_postagem=''):
        self.id_lpl = id_lpl
        self.valor_global = valor_global
        self.mcu_unidade_postagem = mcu_unidade_postagem
        self.nome_unidade_postagem = nome_unidade_postagem
        self.cartao_postagem = cartao_postagem

    def get_xml(self):
        xml = u'<plp>'

        aux = str(self.id_lpl) if self.id_lpl else ''
        xml += u'<id_plp>%s</id_plp>' % aux

        aux = str(self.valor_global) if self.valor_global else ''
        xml += u'<valor_global>%s</valor_global>' % aux

        xml += u'<mcu_unidade_postagem>%s</mcu_unidade_postagem>' % \
               self.mcu_unidade_postagem
        xml += u'<nome_unidade_postagem>%s</nome_unidade_postagem>' %  \
               self.nome_unidade_postagem
        xml += u'<cartao_postagem>%s</cartao_postagem>' % self.cartao_postagem
        xml += u'</plp>'

        TagPLP.validar_xml(xml)
        return xml

    @staticmethod
    def validar_xml(xml):
        import plp_xml_validator

        if plp_xml_validator.validate_xml(xml):
            print u'XML TagPLP validado com sucesso!'
        else:
            print u'Validação de XML TagPLP falhou!'
<|endoftext|>


<commit_before>
from autotune import *

# --------------------------------------------------------------------------------------------------------------
# Unit Tests
# --------------------------------------------------------------------------------------------------------------

def test_crossover(verbose=False):
    (f, g) = test_funcs()
    constraints = Constraints()

    for j in range(8):
        random.seed(j)

#        for i in range(1000):
#            a = nontrivial_schedule(g)
#            a.apply()
#        print 'random_schedule:  OK'

        a = nontrivial_schedule(g)
        aL = str(a).split('\n')
        while True:
            b = nontrivial_schedule(g)
            bL = str(b).split('\n')
            all_different = True
            assert len(aL) == len(bL), (a, b, aL, bL)
            for i in range(len(aL)):
                if aL[i] == bL[i]:
                    all_different = False
                    break
            if all_different:
                break
        #print 'test_crossover'
        #print '----- Schedule A -----'
        #print a
        #print
        #print '----- Schedule B -----'
        #print b
        #T0 = time.time()
        #print 'a'
        for i in range(80):
            c = crossover(a, b, constraints)
            c.apply(constraints)
            cL = str(c).split('\n')
            assert aL != bL and aL != cL and bL != cL
            if verbose:
                print '---- Crossover %d,%d ----'%(j,i)
                print c
        #T1 = time.time()

        p = AutotuneParams()
        #print 'b'
        for i in range(80):
            if verbose:
                print '---- Mutate after crossover %d,%d ----'%(j,i)
                print 'a', repr(str(a)), a.new_vars()
                print 'b', repr(str(b)), b.new_vars()
            c = crossover(a, b, constraints)
            if verbose:
                print 'c', repr(str(c)), c.new_vars()
            c.apply(constraints)
            c = mutate(c, p, constraints)
            if verbose:
                print 'cmutate', repr(str(c)), c.new_vars()
            c.apply(constraints)
    #        cL = str(c).split('\n')
    #        assert aL != bL and aL != cL and bL != cL

        #T2 = time.time()
        #print T1-T0, T2-T1

        def test_generation(L, prevL):
            assert len(L) == p.population_size
            for x in L:
                #print '-'*40
                #print x.title()
                #print x
                x.apply(constraints)
            current_set = set(str(x) for x in L)
            prev_set = set(str(x) for x in prevL)
            assert len(current_set) > 2
            if len(prev_set):
                assert len(current_set & prev_set) >= 1

        #random.seed(2)
        #print 'c'
        prev_gen = []
        for gen in range(2):
            L = next_generation(prev_gen, p, g, constraints, 0)
            if j == 0 and verbose:
                for i in range(len(L)):
                    print 'gen=%d, i=%d'%(gen,i)
                    print L[i]
                    print '-'*20
            test_generation(L, prev_gen)
            prev_gen = L

    print 'crossover:         OK'
    print 'mutate:            OK'
    print 'next_generation:   OK'

def test_funcs(cache=[]):
    if len(cache):
        return cache[0]
    f = halide.Func('f')
    x = halide.Var('x')
    y = halide.Var('y')
    c = halide.Var('c')
    g = halide.Func('g')
    v = halide.Var('v')
    input = halide.UniformImage(halide.UInt(16), 3)
    int_t = halide.Int(32)
    f[x,y,c] = input[halide.clamp(x,halide.cast(int_t,0),halide.cast(int_t,input.width()-1)),
                     halide.clamp(y,halide.cast(int_t,0),halide.cast(int_t,input.height()-1)),
                     halide.clamp(c,halide.cast(int_t,0),halide.cast(int_t,2))]
    #g[v] = f[v,v]
    g[x,y,c] = f[x,y,c]+1
    cache.append((f, g))
    return (f, g)

def nontrivial_schedule(g):
    while True:
        r = random_schedule(g, 0, DEFAULT_MAX_DEPTH)
        s = str(r)
        if len(s) > 30:
            contains_num = False
            for i in range(65):
                if str(i) in s:
                    contains_num = True
                    break
            if contains_num:
                return r

def test_sample_prob():
    d = {'a': 0.3, 'b': 0.5, 'c': 0.2, 'd': 0.0}
    count = 10000
    dc = {'a': 0, 'b': 0, 'c': 0, 'd': 0}
    for i in range(count):
        key = sample_prob(d)
        dc[key] += 1
    eps = 0.05
    for key in d.keys():
        assert abs(dc[key]*1.0/count-d[key])<eps, (key, d[key], dc[key]*1.0/count)
    assert dc['d'] == 0
    print 'sample_prob:       OK'

def test_schedules(verbose=False, test_random=False):
    #random_module.seed(int(sys.argv[1]) if len(sys.argv)>1 else 0)
    constraints = Constraints()
    halide.exit_on_signal()
    (f, g) = test_funcs()
    assert sorted(halide.all_vars(g).keys()) == sorted(['x', 'y', 'c']) #, 'v'])

    if verbose:
        print halide.func_varlist(f)
        print 'caller_vars(f) =', caller_vars(g, f)
        print 'caller_vars(g) =', caller_vars(g, g)

#    validL = list(valid_schedules(g, f, 4))
#    validL = [repr(_x) for _x in validL]
#
#    for L in sorted(validL):
#        print repr(L)
    T0 = time.time()
    if not test_random:
        random = True #False
        nvalid_determ = 0
        for L in schedules_func(g, f, 0, 3):
            nvalid_determ += 1
            if verbose:
                print L
        nvalid_random = 0
        for i in range(100):
            for L in schedules_func(g, f, 0, DEFAULT_MAX_DEPTH, random=True): #sorted([repr(_x) for _x in valid_schedules(g, f, 3)]):
                if verbose and 0:
                    print L#repr(L)
                nvalid_random += 1
    s = []
    for i in range(1000):
        d = random_schedule(g, 1, DEFAULT_MAX_DEPTH)
        si = str(d)
        si2 = str(Schedule.fromstring(g, si))
        if si != si2:
            print '-'*40
            print 'd'
            print d
            print '-'*40
            print 'si'
            print si
            print '-'*40
            print 'si2'
            print si2
            raise ValueError

        s.append(si)
        if verbose:
            print 'Schedule:'
            print '-'*20
            print si
            print '-'*20
            print
        sys.stdout.flush()
        d.apply(constraints)
        if test_random:
            evaluate = d.test((36, 36, 3), input, Constraints())
            print 'evaluate'
            evaluate()
            if test_random:
                print 'Success'
                sys.exit()
    T1 = time.time()

    s = '\n'.join(s)
    assert 'f.chunk(_c0)' in s
    assert 'f.root().vectorize' in s
    assert 'f.root().unroll' in s
    assert 'f.root().split' in s
    assert 'f.root().tile' in s
    assert 'f.root().parallel' in s
    assert 'f.root().reorder' in s

    assert nvalid_random == 100
    if verbose:
        print 'generated in %.3f secs' % (T1-T0)

    print 'random_schedule:   OK'

    r = nontrivial_schedule(g)
    constantL = [str(r)]
#    print 'Randomizing the constants in a chosen schedule:'
    for i in range(100):
        #print r.randomized_const()
        #print
        constantL.append(str(r.randomized_const()))
    assert len(set(constantL)) > 1
    print 'randomized_const:  OK'

def test():
    random.seed(0)
    test_sample_prob()
#    test_schedules(True)
    test_schedules()
    test_crossover()
<commit_msg>py bindings -- make unit tests work<commit_after>
from autotune import *

# --------------------------------------------------------------------------------------------------------------
# Unit Tests
# --------------------------------------------------------------------------------------------------------------

def test_crossover(verbose=False):
    (f, g, locals_d) = test_funcs()
    constraints = Constraints()

    for j in range(8):
        random.seed(j)

#        for i in range(1000):
#            a = nontrivial_schedule(g)
#            a.apply()
#        print 'random_schedule:  OK'

        a = nontrivial_schedule(g)
        aL = str(a).split('\n')
        while True:
            b = nontrivial_schedule(g)
            bL = str(b).split('\n')
            all_different = True
            assert len(aL) == len(bL), (a, b, aL, bL)
            for i in range(len(aL)):
                if aL[i] == bL[i]:
                    all_different = False
                    break
            if all_different:
                break
        #print 'test_crossover'
        #print '----- Schedule A -----'
        #print a
        #print
        #print '----- Schedule B -----'
        #print b
        #T0 = time.time()
        #print 'a'
        for i in range(80):
            c = crossover(a, b, constraints)
            c.apply(constraints)
            cL = str(c).split('\n')
            assert aL != bL and aL != cL and bL != cL
            if verbose:
                print '---- Crossover %d,%d ----'%(j,i)
                print c
        #T1 = time.time()

        p = AutotuneParams()
        #print 'b'
        for i in range(80):
            if verbose:
                print '---- Mutate after crossover %d,%d ----'%(j,i)
                print 'a', repr(str(a)), a.new_vars()
                print 'b', repr(str(b)), b.new_vars()
            c = crossover(a, b, constraints)
            if verbose:
                print 'c', repr(str(c)), c.new_vars()
            c.apply(constraints)
            c = mutate(c, p, constraints)
            if verbose:
                print 'cmutate', repr(str(c)), c.new_vars()
            c.apply(constraints)
    #        cL = str(c).split('\n')
    #        assert aL != bL and aL != cL and bL != cL

        #T2 = time.time()
        #print T1-T0, T2-T1

        def test_generation(L, prevL):
            assert len(L) == p.population_size
            for x in L:
                #print '-'*40
                #print x.title()
                #print x
                x.apply(constraints)
            current_set = set(str(x) for x in L)
            prev_set = set(str(x) for x in prevL)
            assert len(current_set) > 2
            if len(prev_set):
                assert len(current_set & prev_set) >= 1

        #random.seed(2)
        #print 'c'
        prev_gen = []
        for gen in range(2):
            L = next_generation(prev_gen, p, g, constraints, 0, [{'time': 0.1} for i in range(len(prev_gen))])
            if j == 0 and verbose:
                for i in range(len(L)):
                    print 'gen=%d, i=%d'%(gen,i)
                    print L[i]
                    print '-'*20
            test_generation(L, prev_gen)
            prev_gen = L

    print 'crossover:         OK'
    print 'mutate:            OK'
    print 'next_generation:   OK'

def test_funcs(cache=[]):
    if len(cache):
        return cache[0]
    f = halide.Func('f')
    x = halide.Var('x')
    y = halide.Var('y')
    c = halide.Var('c')
    g = halide.Func('g')
    v = halide.Var('v')
    input = halide.UniformImage(halide.UInt(16), 3)
    int_t = halide.Int(32)
    f[x,y,c] = input[halide.clamp(x,halide.cast(int_t,0),halide.cast(int_t,input.width()-1)),
                     halide.clamp(y,halide.cast(int_t,0),halide.cast(int_t,input.height()-1)),
                     halide.clamp(c,halide.cast(int_t,0),halide.cast(int_t,2))]
    #g[v] = f[v,v]
    g[x,y,c] = f[x,y,c]+1
    cache.append((f, g, locals()))
    return (f, g, locals())

def nontrivial_schedule(g):
    while True:
        r = random_schedule(g, 0, DEFAULT_MAX_DEPTH)
        s = str(r)
        if len(s) > 30:
            contains_num = False
            for i in range(65):
                if str(i) in s:
                    contains_num = True
                    break
            if contains_num:
                return r

def test_sample_prob():
    d = {'a': 0.3, 'b': 0.5, 'c': 0.2, 'd': 0.0}
    count = 10000
    dc = {'a': 0, 'b': 0, 'c': 0, 'd': 0}
    for i in range(count):
        key = sample_prob(d)
        dc[key] += 1
    eps = 0.05
    for key in d.keys():
        assert abs(dc[key]*1.0/count-d[key])<eps, (key, d[key], dc[key]*1.0/count)
    assert dc['d'] == 0
    print 'sample_prob:       OK'

def test_schedules(verbose=False, test_random=False):
    #random_module.seed(int(sys.argv[1]) if len(sys.argv)>1 else 0)
    constraints = Constraints()
    halide.exit_on_signal()
    (f, g, locals_d) = test_funcs()
    assert sorted(halide.all_vars(g).keys()) == sorted(['x', 'y', 'c']) #, 'v'])

    if verbose:
        print halide.func_varlist(f)
        print 'caller_vars(f) =', caller_vars(g, f)
        print 'caller_vars(g) =', caller_vars(g, g)

#    validL = list(valid_schedules(g, f, 4))
#    validL = [repr(_x) for _x in validL]
#
#    for L in sorted(validL):
#        print repr(L)
    T0 = time.time()
    if not test_random:
        random = True #False
        nvalid_determ = 0
        for L in schedules_func(g, f, 0, 3):
            nvalid_determ += 1
            if verbose:
                print L

    nvalid_random = 0
    for i in range(100):
        for L in schedules_func(g, f, 0, DEFAULT_MAX_DEPTH, random=True): #sorted([repr(_x) for _x in valid_schedules(g, f, 3)]):
            if verbose and 0:
                print L#repr(L)
            nvalid_random += 1

    s = []
    for i in range(1000):
        d = random_schedule(g, 1, DEFAULT_MAX_DEPTH)
        si = str(d)
        si2 = str(Schedule.fromstring(g, si))
        if si != si2:
            print '-'*40
            print 'd'
            print d
            print '-'*40
            print 'si'
            print si
            print '-'*40
            print 'si2'
            print si2
            raise ValueError

        s.append(si)
        if verbose:
            print 'Schedule:'
            print '-'*20
            print si
            print '-'*20
            print
        sys.stdout.flush()
        d.apply(constraints)
        #if test_random:
        #    evaluate = d.test((36, 36, 3), locals_d['input'], Constraints())
        #    print 'evaluate'
        #    evaluate()
        #    if test_random:
        #        print 'Success'
        #        sys.exit()
    T1 = time.time()

    s = '\n'.join(s)
    assert 'f.chunk(_c0)' in s
    assert 'f.root().vectorize' in s
    assert 'f.root().unroll' in s
    assert 'f.root().split' in s
    assert 'f.root().tile' in s
    assert 'f.root().parallel' in s
    assert 'f.root().reorder' in s

    assert nvalid_random == 100
    if verbose:
        print 'generated in %.3f secs' % (T1-T0)

    print 'random_schedule:   OK'

    r = nontrivial_schedule(g)
    constantL = [str(r)]
#    print 'Randomizing the constants in a chosen schedule:'
    for i in range(100):
        #print r.randomized_const()
        #print
        constantL.append(str(r.randomized_const()))
    assert len(set(constantL)) > 1
    print 'randomized_const:  OK'

def test():
    random.seed(0)
    test_sample_prob()
#    test_schedules(True)
    test_schedules(test_random=True)
    test_crossover()
<|endoftext|>


<commit_before># What is the smallest number that can be divided evenly by all the numbers from 1-20?

from __future__ import print_function

import timeit

from functools import partial
from operator import mod

try:
    range = xrange
except NameError:
    pass

start = timeit.default_timer()

def euler_5():
    dividend = 20
    divisors = range(1, 20)
    while True:
        if (dividend % 7 != 0 or dividend % 9 != 0 or dividend % 3 != 0 or
            sum(map(partial(mod, dividend), divisors)) != 0):
            dividend += 20
        else:
            return dividend

print("Answer: {}".format(euler_5()))
stop = timeit.default_timer()
print("Time: {0:9.5f}".format(stop - start))
<commit_msg>As well as other definite factors of multiples of 20 (heuristic)<commit_after># What is the smallest number that can be divided evenly by all the numbers from 1-20?

from __future__ import print_function

import timeit

from functools import partial
from operator import mod

try:
    range = xrange
except NameError:
    pass

start = timeit.default_timer()

def euler_5():
    dividend = 20
    divisors = [6, 8, 11, 12, 13, 14, 15, 16, 17, 18, 19]
    while True:
        if (dividend % 7 != 0 or dividend % 9 != 0 or dividend % 3 != 0 or
            sum(map(partial(mod, dividend), divisors)) != 0):
            dividend += 20
        else:
            return dividend

print("Answer: {}".format(euler_5()))
stop = timeit.default_timer()
print("Time: {0:9.5f}".format(stop - start))
<|endoftext|>


<commit_before>#! /usr/bin/env python
"""WebTAG Parser - functionality for extracting from the Data Book.

This class parses the databook to a more convenient JSON format for
use by :py:class:`~.WebTagData`.

"""
from __future__ import print_function
import argparse
import datetime
import json
from os import path
from sys import argv, stdout

import xlrd

class WebTagParser(object):
    """Class to handle access to a WebTAG Databook Excel file.

    The class is designed to operate as a context manager if needed.

    Notes:
      Where possible, the workbook is opened in ``on_demand`` mode, to
      avoid loading all worksheets at once. :py:meth:`extract_data`
      will load and unload the appropriate worksheet as required.

    Arguments:
      filename (``str``): The WebTAG Databook file to open.

    Attributes:
      book (``xlrd.Workbook``): The Excel workbook.
      date (``datetime.datetime``): The release date of the databook.
      filename (``str``): The name of the file to open.
      version (``str``): The version of the databook.

    """

    BASE = ("User Parameters", 0, 11, "Price year")
    CHECK = ("Cover", 2, 0, "WebTAG Databook")
    DATE = ("Audit", 0, 2)
    LOCATIONS = {"discount_rate": ("A1.1.1", 24, 1, 3),
                 "rail_diesel_price": ("A1.3.7", 27, 1, 5),
                 "rail_electricity_price": ("A1.3.7", 27, 1, 7),
                 "rail_fuel_duty": ("A1.3.7", 27, 1, 10),
                 "gdp_growth": ("Annual Parameters", 30, 1, 5)}

    def __init__(self, filename):
        self.filename = filename
        sht, row, col, val = self.CHECK
        err_msg = "Not a WebTAG Databook."
        try:
            self.book = xlrd.open_workbook(filename, on_demand=True)
            sheet = self.book.sheet_by_name(sht)
        except xlrd.XLRDError:
            raise IOError(err_msg)
        try:
            compare = sheet.cell(row, col).value
        except IndexError:
            raise IOError(err_msg)
        if compare != val:
            raise IOError(err_msg)
        self.version = sheet.cell(3, 0).value
        self.date = self._extract_date(*self.DATE)
        self.base_year = self._extract_base(*self.BASE)

    def _extract_base(self, sheet_name, label_col, base_col, label):
        """Extract the base year from the appropriate worksheet.

        Arguments:
          sheet_name (``str``): The worksheet from which to extract
            the base year.
          label_col (``int``): The column in which to find the label.
          base_col (``int``): The column from which to extract the base
            year.
          label (``str``): The label to find in ``label_col``.

        Returns:
          ``int``: The base year for the data in the workbook.

        """
        sheet = self.book.sheet_by_name(sheet_name)
        base_row = sheet.col_values(label_col).index(label)
        base = sheet.col_values(base_col)[base_row]
        self.book.unload_sheet(sheet_name)
        return int(base)

    def _extract_date(self, sheet_name, version_col, date_col):
        """Extract the version's date from the appropriate worksheet.

        Notes:
          Expects that the date will be in the date_col on the same row
          as the version appears in version_col.

        Arguments:
          date_col (``int``): The column from which to extract the
            date.
          sheet_name (``str``): The worksheet from which to extract the
            date.
          version_col (``int``): The column in which to find the
            version.

        Returns:
          ``datetime.date``: The release date of the workbook.

        """
        sheet = self.book.sheet_by_name(sheet_name)
        date_row = sheet.col_values(version_col).index(self.version)
        date = xlrd.xldate_as_tuple(sheet.col_values(date_col)[date_row],
                                    self.book.datemode)
        self.book.unload_sheet(sheet_name)
        return datetime.date(*date[:3])

    def __enter__(self):
        return self

    def __exit__(self, *args):
        self.book.release_resources()

    def close(self):
        """Close the file release the :py:attr:`book` resources."""
        self.__exit__()

    def extract_all(self, verbose=False):
        """Extract all data from ``LOCATIONS`` and useful metadata."""
        data = {}
        for name in self.LOCATIONS:
            if verbose:
                print("Extracting {}".format(name))
            data[name] = self.extract_named_data(name)
        if verbose:
            print("Extracting metadata")
        data["source"] = path.split(self.filename)[-1]
        data["released"] = str(self.date)
        data["version"] = self.version
        data["base_year"] = self.base_year
        return data

    def extract_data(self, sheet_name, start_row, label_col, value_col):
        """Extract data from the specified worksheet."""
        sheet = self.book.sheet_by_name(sheet_name)
        labels = sheet.col_values(label_col)[start_row:]
        values = sheet.col_values(value_col)[start_row:]
        try:
            data = {int(k): v for k, v in zip(labels, values) if k}
        except ValueError:
            data = {k: v for k, v in zip(labels, values) if k}
        self.book.unload_sheet(sheet_name)
        data["title"] = sheet.cell(2, 0).value
        data["table"] = sheet.cell(3, 0).value
        return data

    def extract_named_data(self, name):
        """Extract a named data series from ``LOCATIONS``."""
        return self.extract_data(*self.LOCATIONS[name])

def parse_args(args):
    """Parse the arguments for :py:func:`cli`.

    Arguments:
      args (``list`` of ``str``): Arguments from command line.

    Returns:
      ``argparse.Namespace``: The parsed arguments.

    Raises:
      ArgumentError: If ``-v`` is supplied without ``-o``.

    """
    description = "Convert a WebTAG Databook to JSON"
    arg_parser = argparse.ArgumentParser(description=description)
    arg_parser.add_argument("file",
                            help="file to parse")
    group1 = arg_parser.add_argument_group("Output to file",
                                           "Choose file rather than pipe.")
    group1.add_argument("-o",
                        help="file to output to")
    group1.add_argument("-v", "--verbose",
                        action="store_true",
                        help="increase output verbosity")
    args_ = arg_parser.parse_args(args)
    if args_.o is None and args_.verbose:
        msg = "-v cannot be used without -o"
        arg_parser.error(msg)
    return args_

def cli(args):
    """Provide a CLI for the :py:class:`~.WebTagParser`.

    Notes:
      Will either output to a specified file (with optional verbose
      reporting) or dump the JSON data to ``stdout``.

    Arguments:
      args (``argparse.Namespace``): The parsed command line arguments.

    Raises:
      ValueError: If ``-v`` is supplied without ``-o``.

    """
    print(args.file, args.o, args.verbose)
    if args.o is None and args.verbose:
        raise ValueError("Verbose mode not supported unless output file set.")
    if args.verbose:
        print("Reading from input file {}".format(args.file))
    with WebTagParser(args.file) as parser:
        data = parser.extract_all(args.verbose)
        if args.verbose:
            print("Data extracted from input file")
    if args.o is not None:
        with open(args.o, "w") as outfile:
            if args.verbose:
                print("Writing to output file {}".format(args.o))
            json.dump(data, outfile, indent=4)
    else:
        stdout.write(json.dumps(data, indent=4))

if __name__ == "__main__":
    cli(parse_args(argv[1:]))
<commit_msg>Refactored version to match base year and date<commit_after>#! /usr/bin/env python
"""WebTAG Parser - functionality for extracting from the Data Book.

This class parses the databook to a more convenient JSON format for
use by :py:class:`~.WebTagData`.

"""
from __future__ import print_function
import argparse
import datetime
import json
from os import path
from sys import argv, stdout

import xlrd

class WebTagParser(object):
    """Class to handle access to a WebTAG Databook Excel file.

    The class is designed to operate as a context manager if needed.

    Where possible, the workbook is opened in ``on_demand`` mode, to
    avoid loading all worksheets at once. :py:meth:`extract_data` will
    load and unload the appropriate worksheets as required.

    Arguments:
      filename (``str``): The WebTAG Databook file to open.

    Attributes:
      book (``xlrd.Workbook``): The Excel workbook.
      date (``datetime.datetime``): The release date of the databook.
      filename (``str``): The name of the file to open.
      version (``str``): The version of the databook.

    """

    BASE = ("User Parameters", 0, 11, "Price year")
    CHECK = ("Cover", 2, 0, "WebTAG Databook")
    DATE = ("Audit", 0, 2)
    LOCATIONS = {"discount_rate": ("A1.1.1", 24, 1, 3),
                 "rail_diesel_price": ("A1.3.7", 27, 1, 5),
                 "rail_electricity_price": ("A1.3.7", 27, 1, 7),
                 "rail_fuel_duty": ("A1.3.7", 27, 1, 10),
                 "gdp_growth": ("Annual Parameters", 30, 1, 5)}
    VERSION = ("Cover", 3, 0)

    def __init__(self, filename):
        self.filename = filename
        sht, row, col, val = self.CHECK
        err_msg = "Not a WebTAG Databook."
        try:
            self.book = xlrd.open_workbook(filename, on_demand=True)
            sheet = self.book.sheet_by_name(sht)
        except xlrd.XLRDError:
            raise IOError(err_msg)
        try:
            compare = sheet.cell(row, col).value
        except IndexError:
            raise IOError(err_msg)
        if compare != val:
            raise IOError(err_msg)
        self.version = self._extract_version(*self.VERSION)
        self.date = self._extract_date(*self.DATE)
        self.base_year = self._extract_base_year(*self.BASE)

    def _extract_base_year(self, sheet_name, label_col, base_col, label):
        """Extract the base year from the appropriate worksheet.

        Arguments:
          sheet_name (``str``): The worksheet from which to extract
            the base year.
          label_col (``int``): The column in which to find the label.
          base_col (``int``): The column from which to extract the base
            year.
          label (``str``): The label to find in ``label_col``.

        Returns:
          ``int``: The base year for the data in the workbook.

        """
        sheet = self.book.sheet_by_name(sheet_name)
        base_row = sheet.col_values(label_col).index(label)
        base = sheet.col_values(base_col)[base_row]
        self.book.unload_sheet(sheet_name)
        return int(base)

    def _extract_date(self, sheet_name, version_col, date_col):
        """Extract the version's date from the appropriate worksheet.

        Expects that the date will be in the ``date_col`` on the same
        row as the version appears in ``version_col``.

        Arguments:
          date_col (``int``): The column from which to extract the
            date.
          sheet_name (``str``): The worksheet from which to extract the
            date.
          version_col (``int``): The column in which to find the
            version.

        Returns:
          ``datetime.date``: The release date of the workbook.

        """
        sheet = self.book.sheet_by_name(sheet_name)
        date_row = sheet.col_values(version_col).index(self.version)
        date = xlrd.xldate_as_tuple(sheet.col_values(date_col)[date_row],
                                    self.book.datemode)
        self.book.unload_sheet(sheet_name)
        return datetime.date(*date[:3])

    def _extract_version(self, sheet_name, row, col):
        """Extract the version from the appropriate worksheet.

        Arguments:
          sheet_name (``str``): The worksheet from which to extract
            the version.
          row (``int``): The row in which to find the version.
          col (``int``): The column in which to find the version.
            year.

        Returns:
          ``str``: The version of the workbook.

        """
        sheet = self.book.sheet_by_name(sheet_name)
        version = sheet.cell(row, col).value
        self.book.unload_sheet(sheet_name)
        return version

    def __enter__(self):
        return self

    def __exit__(self, *args):
        self.book.release_resources()

    def close(self):
        """Release the :py:attr:`book` resources."""
        self.__exit__()

    def extract_all(self, verbose=False):
        """Extract all data from ``LOCATIONS`` and useful metadata.

        Arguments:
          verbose (``bool``, optional): Whether to report progress.
            Implemented primarily for :py:func:`cli` usage. Defaults
            to ``False``.

        Returns:
          dict: The data extracted from the :py:attr:`book`.

        """
        data = {}
        for name in self.LOCATIONS:
            if verbose:
                print("Extracting {}".format(name))
            data[name] = self.extract_named_data(name)
        if verbose:
            print("Extracting metadata")
        data["source"] = path.split(self.filename)[-1]
        data["released"] = str(self.date)
        data["version"] = self.version
        data["base_year"] = self.base_year
        return data

    def extract_data(self, sheet_name, start_row, key_col, value_col):
        """Extract data from the specified worksheet.

        Assumes that cell ``A3`` contains the worksheet title and that
        cell ``A4`` contains the table name.

        Arguments:
          sheet_name (``str``): The name of the worksheet.
          start_row (``int``): The first row to extract data from.
          key_col (``int``): The column to extract keys from.
          value_col (``int``): The column to extract values from.

        Returns:
          dict: The extracted data

        """
        sheet = self.book.sheet_by_name(sheet_name)
        labels = sheet.col_values(key_col)[start_row:]
        values = sheet.col_values(value_col)[start_row:]
        try:
            data = {int(k): v for k, v in zip(labels, values) if k}
        except ValueError:
            data = {k: v for k, v in zip(labels, values) if k}
        self.book.unload_sheet(sheet_name)
        data["title"] = sheet.cell(2, 0).value
        data["table"] = sheet.cell(3, 0).value
        return data

    def extract_named_data(self, name):
        """Extract a named data series from ``LOCATIONS``.

        Arguments:
          name (``str``): The name of the data series (must be in
            :py:attr:`Locations`).

        Returns:
          dict: The extracted data series.

        Raises:
          KeyError: If ``name`` is not in :py:attr:`Locations`.

        """
        return self.extract_data(*self.LOCATIONS[name])

def parse_args(args):
    """Parse the arguments for :py:func:`cli`.

    Arguments:
      args (``list`` of ``str``): Arguments from command line.

    Returns:
      ``argparse.Namespace``: The parsed arguments.

    Raises:
      ArgumentError: If ``-v`` is supplied without ``-o``.

    """
    description = "Convert a WebTAG Databook to JSON"
    arg_parser = argparse.ArgumentParser(description=description)
    arg_parser.add_argument("file",
                            help="file to parse")
    group1 = arg_parser.add_argument_group("Output to file",
                                           "Choose file rather than pipe.")
    group1.add_argument("-o",
                        help="file to output to")
    group1.add_argument("-v", "--verbose",
                        action="store_true",
                        help="increase output verbosity")
    args_ = arg_parser.parse_args(args)
    if args_.o is None and args_.verbose:
        msg = "-v cannot be used without -o"
        arg_parser.error(msg)
    return args_

def cli(args):
    """Provide a CLI for the :py:class:`~.WebTagParser`.

    Will either output to a specified file (with optional verbose
    reporting) or dump the JSON data to ``stdout``.

    Arguments:
      args (``argparse.Namespace``): The parsed command line arguments.

    Raises:
      ValueError: If ``-v`` is supplied without ``-o``.

    """
    print(args.file, args.o, args.verbose)
    if args.o is None and args.verbose:
        raise ValueError("Verbose mode not supported unless output file set.")
    if args.verbose:
        print("Reading from input file {}".format(args.file))
    with WebTagParser(args.file) as parser:
        data = parser.extract_all(args.verbose)
        if args.verbose:
            print("Data extracted from input file")
    if args.o is not None:
        with open(args.o, "w") as outfile:
            if args.verbose:
                print("Writing to output file {}".format(args.o))
            json.dump(data, outfile, indent=4)
    else:
        stdout.write(json.dumps(data, indent=4))

if __name__ == "__main__":
    cli(parse_args(argv[1:]))
<|endoftext|>


<commit_before>from django.test import TransactionTestCase
from django.core.urlresolvers import reverse

from pybb.tests.base import SharedTestModule
from pybb.contrib.quotes.models import Quote

from pybb.contrib.quotes.processors import QuotePreProcessor
from pybb.contrib.quotes import settings as quotes_settings

from pybb.models import Post

class ReportsTest(TransactionTestCase, SharedTestModule):
    def setUp(self):
        self.create_user()
        self.create_initial()

    def test_simple_quote(self):
        post = Post(user=self.staff, body='[quote="zeus;%d"]Super quote[/quote]' % self.post.pk, topic=self.topic)
        post.save()

        self.assertEqual(post.body_html, u'

        Posted by zeus

        Super quote

')

        self.assertEqual(Quote.objects.filter(from_post=post).count(), 1)

    def test_double_quote(self):
        post = Post(user=self.staff, body='[quote="%s;%d"]%s[/quote]' % (self.post.user.username,
                                                                         self.post.pk,
                                                                         self.post.body),
                    topic=self.topic)
        post.save()

        post = Post(user=self.superuser, body='[quote="%s;%d"]%s[/quote]' % (post.user.username,
                                                                             post.pk,
                                                                             post.body), topic=self.topic)
        post.save()

        self.assertEqual(post.body_html, u'

        Posted by thoas

        Posted by zeus

        bbcode test

')

        self.assertEqual(Quote.objects.filter(from_post=post).count(), 2)

    def test_multiple_quote_add(self):
        self.login_client(username='thoas', password='$ecret')

        post = Post(topic=self.topic, user=self.superuser, body='[b]do you want my pere nowel?[/b]')
        post.save()

        response = self.client.post(reverse('quote'), data={
            'topic_id': self.topic.pk,
            'post_id': post.pk
        })

        self.assertEqual(response.status_code, 302)

        response = self.client.get(reverse('pybb:post_create',
                                           kwargs={'topic_id': self.topic.id}) + '?quote_id=%d' % self.post.id)
        self.assertEqual(response.context['form'].initial['body'], u'[quote="zeus;1"]bbcode [b]test[/b][/quote]\n\n[quote="oleiade;2"][b]do you want my pere nowel?[/b][/quote]\n')
        self.assertEqual(response.status_code, 200)

        response = self.client.post(reverse('quote'), data={
            'topic_id': self.topic.pk,
            'post_id': post.pk
        })

        self.assertEqual(response.status_code, 302)

        response = self.client.get(reverse('pybb:post_create',
                                           kwargs={'topic_id': self.topic.id}))
        self.assertTrue(not 'body' in response.context['form'].initial)
        self.assertEqual(response.status_code, 200)

    def test_emmbedded_quotes_preprocessor(self):
        body='actual message[quote="zeus;1"]first level[quote="oleiade;2"]second level[quote="zeus;1"]third level[/quote]second level[/quote]first level[/quote]actual message'
        qp = QuotePreProcessor(body=body)
        quotes_settings.PYBB_QUOTES_MAX_DEPTH=1
        self.assertEqual(qp.render(),u'actual message[quote="zeus;1"]first levelfirst level[/quote]actual message')
        quotes_settings.PYBB_QUOTES_MAX_DEPTH=1
        self.assertEqual(qp.render(),u'actual message[quote="zeus;1"]first levelfirst level[/quote]actual message')
        quotes_settings.PYBB_QUOTES_MAX_DEPTH=2
        self.assertEqual(qp.render(),u'actual message[quote="zeus;1"]first level[quote="oleiade;2"]second levelsecond level[/quote]first level[/quote]actual message')
        quotes_settings.PYBB_QUOTES_MAX_DEPTH=3
        self.assertEqual(qp.render(),u'actual message[quote="zeus;1"]first level[quote="oleiade;2"]second level[quote="zeus;1"]third level[/quote]second level[/quote]first level[/quote]actual message')
        quotes_settings.PYBB_QUOTES_MAX_DEPTH=-1
        self.assertEqual(qp.render(),u'actual message[quote="zeus;1"]first level[quote="oleiade;2"]second level[quote="zeus;1"]third level[/quote]second level[/quote]first level[/quote]actual message')

<commit_msg>readded quote processor + tests<commit_after>from django.test import TransactionTestCase
from django.core.urlresolvers import reverse

from pybb.tests.base import SharedTestModule
from pybb.contrib.quotes.models import Quote

from pybb.contrib.quotes.processors import QuotePreProcessor
from pybb.contrib.quotes import settings as quotes_settings

from pybb.models import Post

class ReportsTest(TransactionTestCase, SharedTestModule):
    def setUp(self):
        self.create_user()
        self.create_initial()
        quotes_settings.PYBB_QUOTES_MAX_DEPTH=-1

    def test_simple_quote(self):
        post = Post(user=self.staff, body='[quote="zeus;%d"]Super quote[/quote]' % self.post.pk, topic=self.topic)
        post.save()

        self.assertEqual(post.body_html, u'

        Posted by zeus

        Super quote

')

        self.assertEqual(Quote.objects.filter(from_post=post).count(), 1)

    def test_double_quote(self):
        post = Post(user=self.staff, body='[quote="%s;%d"]%s[/quote]' % (self.post.user.username,
                                                                         self.post.pk,
                                                                         self.post.body),
                    topic=self.topic)
        post.save()

        post = Post(user=self.superuser, body='[quote="%s;%d"]%s[/quote]' % (post.user.username,
                                                                             post.pk,
                                                                             post.body), topic=self.topic)
        post.save()

        self.assertEqual(post.body_html, u'

        Posted by thoas

        Posted by zeus

        bbcode test

')

        self.assertEqual(Quote.objects.filter(from_post=post).count(), 2)

    def test_multiple_quote_add(self):
        self.login_client(username='thoas', password='$ecret')

        post = Post(topic=self.topic, user=self.superuser, body='[b]do you want my pere nowel?[/b]')
        post.save()

        response = self.client.post(reverse('quote'), data={
            'topic_id': self.topic.pk,
            'post_id': post.pk
        })

        self.assertEqual(response.status_code, 302)

        response = self.client.get(reverse('pybb:post_create',
                                           kwargs={'topic_id': self.topic.id}) + '?quote_id=%d' % self.post.id)
        self.assertEqual(response.context['form'].initial['body'], u'[quote="zeus;1"]bbcode [b]test[/b][/quote]\n\n[quote="oleiade;2"][b]do you want my pere nowel?[/b][/quote]\n')
        self.assertEqual(response.status_code, 200)

        response = self.client.post(reverse('quote'), data={
            'topic_id': self.topic.pk,
            'post_id': post.pk
        })

        self.assertEqual(response.status_code, 302)

        response = self.client.get(reverse('pybb:post_create',
                                           kwargs={'topic_id': self.topic.id}))
        self.assertTrue(not 'body' in response.context['form'].initial)
        self.assertEqual(response.status_code, 200)

    def test_emmbedded_quotes_preprocessor(self):
        body='actual message[quote="zeus;1"]first level[quote="oleiade;2"]second level[quote="zeus;1"]third level[/quote]second level[/quote]first level[/quote]actual message'
        qp = QuotePreProcessor(body=body)
        # test all depths
        quotes_settings.PYBB_QUOTES_MAX_DEPTH=0
        self.assertEqual(qp.render(), u'actual messageactual message')
        quotes_settings.PYBB_QUOTES_MAX_DEPTH=1
        self.assertEqual(qp.render(), u'actual message[quote="zeus;1"]first levelfirst level[/quote]actual message')
        quotes_settings.PYBB_QUOTES_MAX_DEPTH=2
        self.assertEqual(qp.render(), u'actual message[quote="zeus;1"]first level[quote="oleiade;2"]second levelsecond level[/quote]first level[/quote]actual message')
        quotes_settings.PYBB_QUOTES_MAX_DEPTH=3
        self.assertEqual(qp.render(), u'actual message[quote="zeus;1"]first level[quote="oleiade;2"]second level[quote="zeus;1"]third level[/quote]second level[/quote]first level[/quote]actual message')
        # check that higher depth do not break
        quotes_settings.PYBB_QUOTES_MAX_DEPTH=4
        self.assertEqual(qp.render(), u'actual message[quote="zeus;1"]first level[quote="oleiade;2"]second level[quote="zeus;1"]third level[/quote]second level[/quote]first level[/quote]actual message')
        # negative depth deactivate the render
        quotes_settings.PYBB_QUOTES_MAX_DEPTH=-1
        self.assertEqual(qp.render(), u'actual message[quote="zeus;1"]first level[quote="oleiade;2"]second level[quote="zeus;1"]third level[/quote]second level[/quote]first level[/quote]actual message')

<|endoftext|>


<commit_before>from pychecker2.Check import Check
from pychecker2.Options import Opt, BoolOpt
from pychecker2.Warning import Warning
from pychecker2.util import *
from pychecker2 import symbols

from compiler import ast, walk

def _is_method(scope):
    return scope.__class__ is symbols.FunctionScope and \
           scope.parent.__class__ is symbols.ClassScope

def _is_self(scope, name):
    return _is_method(scope) and name in scope.node.argnames[:1]

def is_arg_and_defaulted_to_same_name(name, scope):
    if isinstance(scope, symbols.FunctionScope):
        if name in scope.node.argnames and scope.node.defaults:
            # compute default args
            args = scope.node.argnames[:]
            # knock off kwargs
            if scope.node.kwargs:
                args = args[:-1]
            # knock off varags
            if scope.node.varargs:
                args = args[:-1]
            # take the last args as the defaults
            args = args[-len(scope.node.defaults):]
            try:
                # get the corresponding default arg value
                default = scope.node.defaults[args.index(name)]
                # must be a Name node of the same name
                if isinstance(default, ast.Name) and \
                   default.name == name:
                    return 1
            except ValueError:
                pass
    return None

class ShadowCheck(Check):
    """Use symbol information to check that no scope defines a name
    already known to a parent scope"""

    defineNone = Warning('Report any redefinition of None',
                         'Do not redefine None')
    shadowBuiltins = Warning('Report names that shadow builtins',
                            'Identifier (%s) shadows builtin', 0)
    shadowIdentifier = Warning('Report names already defined in outer scopes',
                               'Identifier (%s) shadows definition in scope %s')
    def check(self, file, unused_checker):
        # warn if any name defined in a scope is defined in a parent scope
        # or even the builtins
        for node, scope in file.not_class_scopes():
            for name in scope.defs:
                if name == 'None':
                    file.warning(scope.defs[name], self.defineNone)
                    continue
                if name in scope.globals:
                    continue
                if is_arg_and_defaulted_to_same_name(name, scope):
                    continue
                if _is_self(scope, name):
                    continue
                if __builtins__.has_key(name):
                    file.warning(scope.defs[name], self.shadowBuiltins, name)
                for p in parents(scope):
                    if p.defs.has_key(name) and \
                       not isinstance(p, symbols.ClassScope):
                        file.warning(scope.defs[name], \
                                     self.shadowIdentifier, name, `p`)

def _str_value(s):
    if type(s) == type(''):
        return eval(s)
    return s

def _empty_function(stmts):
    if not stmts:
        return 1
    stmt = stmts[0]
    # functions which only raise exceptions
    if isinstance(stmt, ast.Raise):
        return 1
    # functions which do nothing
    if len(stmts) == 1 and isinstance(stmt, ast.Pass):
        return 1
    # functions which just return a constant
    if len(stmts) == 1 and \
       isinstance(stmt, ast.Return) and isinstance(stmt.value, ast.Const):
        return 1
    # functions which only assert falsehood
    if isinstance(stmt, ast.Assert):
        if (isinstance(stmt.test, ast.Const) and not stmt.test.value) or \
            (isinstance(stmt.test, ast.Name) and stmt.test.name == 'None'):
            return 1
    return 0

class UnusedCheck(Check):
    """Use symbol information to check that no scope defines a name
    not used in this or any child scope"""

    unused = Warning('Report names not used', 'Identifier (%s) not used')

    def __init__(self):
        self.reportUnusedSelf = None
        self.unusedPrefixes = None

    def get_options(self, options):
        desc = 'Ignore unused identifiers that start with these values'
        default = ['unused', 'empty', 'dummy',
                   '__pychecker__', '__all__', '__version__', 'ignored']
        options.add(Opt(self, 'unusedPrefixes', desc, default))

        desc = 'Ignore unused method "self" parameter'
        options.add(BoolOpt(self, 'reportUnusedSelf', desc))

    def check(self, file, unused_checker):
        self.unusedPrefixes = _str_value(self.unusedPrefixes)

        def used(name, parent_scope):
            if parent_scope.uses.has_key(name):
                return 1
            for c in parent_scope.get_children():
                if used(name, c):
                    return 1
            return 0

        for nodes, scope in file.not_class_scopes():
            if isinstance(nodes, ast.Function):
                if _empty_function(nodes.code.nodes):
                    continue

            # ensure that every defined variable is used in some scope
            for var in scope.defs:
                # check for method self
                if not self.reportUnusedSelf and _is_self(scope, var):
                    continue

                # ignore names in the root scope which are not imported:
                # class defs, function defs, variables, etc, unless
                # they start with '_'
                if scope == file.root_scope:
                    if not scope.imports.has_key(var):
                        if not var.startswith('_'):
                            continue

                for prefix in self.unusedPrefixes:
                    if var.startswith(prefix):
                        break
                else:
                    if not used(var, scope) and not scope.globals.has_key(var):
                        file.warning(scope.defs[var], self.unused, var)

def _importedName(scope, name):
    if scope.imports.has_key(name):
        return 1
    if scope.parent:
        return _importedName(scope.parent, name)
    return None

class UnknownCheck(Check):
    """Use symbol information to check that no scope uses a name
    not defined in a parent scope"""

    unknown = Warning('Report names that are not defined',
                      'Unknown identifier: %s')

    builtins = {}
    builtins.update(__builtins__)
    builtins['__builtins__'] = __builtins__
    builtins['WindowsError'] = getattr(__builtins__, 'WindowsError', None)

    def check(self, file, unused_checker):

        # if a name used is not found in the defined variables, complain
        for scope in file.scopes.values():
            unknown = dict_minus(scope.uses, scope.defs)
            unknown = dict_minus(unknown, self.builtins)
            for var in unknown:
                for p in parents(scope):
                    if p.defs.has_key(var):
                        break
                else:
                    if not _importedName(scope, var):
                        file.warning(scope.uses[var], self.unknown, var)

def _first_arg_defaulted(function_node):
    count = len(function_node.argnames)
    if function_node.varargs:
        count -= 1
    if function_node.kwargs:
        count -= 1
    if count > 0 and len(function_node.defaults) == count:
        return 1
    return None

class SelfCheck(Check):
    'Check for simple self parameter'

    selfName = Warning('Report any methods whose first argument is not self',
                       'First argument to method %s (%s) is not in %s')
    selfDefault = Warning('Report a self parameter with a default value',
                          'First argument to method %s (%s) has a default value')

    functionSelf = Warning('Report functions (not methods) with '
                           'arguments named "self"',
                           'Argument to function (%s) is "%s"')

    missingSelf = Warning('Report methods without "self"',
                          'Method %s is missing self parameter')

    def get_options(self, options):
        desc = 'Name of self parameter'
        default = ["self", "this", "s"]
        options.add(Opt(self, 'selfNames', desc, default))

        desc = 'Suspicious self parameters'
        self.selfSuspicious = ["self"]
        options.add(Opt(self, 'selfSuspicious', desc, self.selfSuspicious))

    def check(self, file, unused_checker):
        self.selfNames      = _str_value(self.selfNames)
        self.selfSuspicious = _str_value(self.selfSuspicious)

        for node, scope in file.function_scopes():
            args = node.argnames
            name = getattr(scope.node, 'name', 'lambda')
            if _is_method(scope):
                if not args:
                    file.warning(scope.node, self.missingSelf, name)
                else:
                    if args[0] not in self.selfNames:
                        file.warning(scope.node, self.selfName,
                                     name, args[0], `self.selfNames`)
                    if _first_arg_defaulted(scope.node):
                        file.warning(scope.node, self.selfDefault,
                                     name, args[0])
            else:
                for arg in args:
                    if arg in self.selfSuspicious:
                        file.warning(scope.defs[arg], self.functionSelf,
                                     name, arg)

class UnpackCheck(Check):
    'Mark all unpacked variables as used'

    def __init__(self):
        self.unpackedUsed = None

    def get_options(self, options):
        desc = 'Do not treat variables used in tuple assignment as used'
        options.add(BoolOpt(self, 'unpackedUsed', desc, 1))

    def check(self, file, unused_checker):
        if not self.unpackedUsed:
            return

        class Visitor:
            def visitAssTuple(self, node):
                for c in node.getChildNodes():
                    for n in parents(c):
                        try:
                            file.scopes[n].uses[c.name] = node
                            break
                        except (KeyError, AttributeError):
                            pass
            visitAssList = visitAssTuple

        # local args unpacked on the `def' line are used, too
        for scope_node, scope in file.function_scopes():
            for arg in type_filter(scope_node.argnames, tuple):
                for unpacked in ast.flatten(arg):
                    scope.uses[unpacked] = scope.uses.get(unpacked, scope_node)

        if file.root_scope:
            walk(file.root_scope.node, Visitor())

def intersect2(a, b):
    return [i for i in a if i in b]

def intersect(items):
    result = items[0]
    for item in items[1:]:
        result = intersect2(result, item)
    return result

class UsedBeforeSetCheck(Check):

    usedBeforeDefined = \
          Warning('Report variables that may be used before they are defined',
                  'The local %s may be used before defined')

    def check(self, file, unused_checker):
        class Visitor(BaseVisitor):
            def __init__(self, defines = None, uses = None):
                # list of vars defined
                self.defines = []
                # name->node vars used before defined
                self.uses = {}
                if defines is not None:
                    self.defines = defines[:]
                if uses is not None:
                    self.uses = uses.copy()
            def visitFunction(self, n):
                self.defines.append(n.name)

            visitClass = visitFunction
            visitAssName = visitFunction

            def visitListComp(self, n):
                # visit qualifiers before expression
                children = ast.flatten_nodes(n.quals) + [n.expr]
                for c in children:
                    self.visit(c)

            def visitName(self, n):
                if n.name not in self.defines:
                    self.uses[n.name] = self.uses.get(n.name, n)

            def visitIf(self, n):
                if not n.else_:
                    return
                visits = []
                for test, code in n.tests:
                    visits.append(walk(code, Visitor(self.defines, self.uses)))
                visits.append(walk(n.else_, Visitor(self.defines, self.uses)))
                # compute the intersection of defines
                self.defines = intersect([v.defines for v in visits])
                # compute the union of uses, perserving first occurances
                union = {}
                visits.reverse()
                for v in visits:
                    union.update(v.uses)
                union.update(self.uses)
                self.uses = union

        for node, scope in file.function_scopes():
            predefined = ast.flatten(scope.params) + scope.imports.keys()
            visitor = walk(node.code, Visitor(predefined))
            usedBefore = dict_intersect(visitor.uses, scope.defs)
            for name, use_node in usedBefore.items():
                file.warning(use_node, self.usedBeforeDefined, name)
<commit_msg>globals define names that are used don't complain about '_' as unused<commit_after>from pychecker2.Check import Check
from pychecker2.Options import Opt, BoolOpt
from pychecker2.Warning import Warning
from pychecker2.util import *
from pychecker2 import symbols

from compiler import ast, walk

def _is_method(scope):
    return scope.__class__ is symbols.FunctionScope and \
           scope.parent.__class__ is symbols.ClassScope

def _is_self(scope, name):
    return _is_method(scope) and name in scope.node.argnames[:1]

def is_arg_and_defaulted_to_same_name(name, scope):
    if isinstance(scope, symbols.FunctionScope):
        if name in scope.node.argnames and scope.node.defaults:
            # compute default args
            args = scope.node.argnames[:]
            # knock off kwargs
            if scope.node.kwargs:
                args = args[:-1]
            # knock off varags
            if scope.node.varargs:
                args = args[:-1]
            # take the last args as the defaults
            args = args[-len(scope.node.defaults):]
            try:
                # get the corresponding default arg value
                default = scope.node.defaults[args.index(name)]
                # must be a Name node of the same name
                if isinstance(default, ast.Name) and \
                   default.name == name:
                    return 1
            except ValueError:
                pass
    return None

class ShadowCheck(Check):
    """Use symbol information to check that no scope defines a name
    already known to a parent scope"""

    defineNone = Warning('Report any redefinition of None',
                         'Do not redefine None')
    shadowBuiltins = Warning('Report names that shadow builtins',
                            'Identifier (%s) shadows builtin', 0)
    shadowIdentifier = Warning('Report names already defined in outer scopes',
                               'Identifier (%s) shadows definition in scope %s')
    def check(self, file, unused_checker):
        # warn if any name defined in a scope is defined in a parent scope
        # or even the builtins
        for node, scope in file.not_class_scopes():
            for name in scope.defs:
                if name == 'None':
                    file.warning(scope.defs[name], self.defineNone)
                    continue
                if name in scope.globals:
                    continue
                if is_arg_and_defaulted_to_same_name(name, scope):
                    continue
                if _is_self(scope, name):
                    continue
                if __builtins__.has_key(name):
                    file.warning(scope.defs[name], self.shadowBuiltins, name)
                for p in parents(scope):
                    if p.defs.has_key(name) and \
                       not isinstance(p, symbols.ClassScope):
                        file.warning(scope.defs[name], \
                                     self.shadowIdentifier, name, `p`)

def _str_value(s):
    if type(s) == type(''):
        return eval(s)
    return s

def _empty_function(stmts):
    if not stmts:
        return 1
    stmt = stmts[0]
    # functions which only raise exceptions
    if isinstance(stmt, ast.Raise):
        return 1
    # functions which do nothing
    if len(stmts) == 1 and isinstance(stmt, ast.Pass):
        return 1
    # functions which just return a constant
    if len(stmts) == 1 and \
       isinstance(stmt, ast.Return) and isinstance(stmt.value, ast.Const):
        return 1
    # functions which only assert falsehood
    if isinstance(stmt, ast.Assert):
        if (isinstance(stmt.test, ast.Const) and not stmt.test.value) or \
            (isinstance(stmt.test, ast.Name) and stmt.test.name == 'None'):
            return 1
    return 0

class UnusedCheck(Check):
    """Use symbol information to check that no scope defines a name
    not used in this or any child scope"""

    unused = Warning('Report names not used', 'Identifier (%s) not used')

    def __init__(self):
        self.reportUnusedSelf = None
        self.unusedPrefixes = None

    def get_options(self, options):
        desc = 'Ignore unused identifiers that start with these values'
        default = ['unused', 'empty', 'dummy',
                   '__pychecker__', '__all__', '__version__', 'ignored']
        options.add(Opt(self, 'unusedPrefixes', desc, default))

        desc = 'Ignore unused method "self" parameter'
        options.add(BoolOpt(self, 'reportUnusedSelf', desc))

    def check(self, file, unused_checker):
        self.unusedPrefixes = _str_value(self.unusedPrefixes)

        def used(name, parent_scope):
            if parent_scope.uses.has_key(name):
                return 1
            for c in parent_scope.get_children():
                if used(name, c):
                    return 1
            return 0

        for nodes, scope in file.not_class_scopes():
            if isinstance(nodes, ast.Function):
                if _empty_function(nodes.code.nodes):
                    continue

            # ensure that every defined variable is used in some scope
            for var in scope.defs:
                # ignore '_'... just because
                if var == '_':
                    continue

                # check for method self
                if not self.reportUnusedSelf and _is_self(scope, var):
                    continue

                # ignore names in the root scope which are not imported:
                # class defs, function defs, variables, etc, unless
                # they start with '_'
                if scope == file.root_scope:
                    if not scope.imports.has_key(var):
                        if not var.startswith('_'):
                            continue

                for prefix in self.unusedPrefixes:
                    if var.startswith(prefix):
                        break
                else:
                    if not used(var, scope) and not scope.globals.has_key(var):
                        file.warning(scope.defs[var], self.unused, var)

def _importedName(scope, name):
    if scope.imports.has_key(name):
        return 1
    if scope.parent:
        return _importedName(scope.parent, name)
    return None

class UnknownCheck(Check):
    """Use symbol information to check that no scope uses a name
    not defined in a parent scope"""

    unknown = Warning('Report names that are not defined',
                      'Unknown identifier: %s')

    builtins = {}
    builtins.update(__builtins__)
    builtins['__builtins__'] = __builtins__
    builtins['WindowsError'] = getattr(__builtins__, 'WindowsError', None)

    def check(self, file, unused_checker):

        # if a name used is not found in the defined variables, complain
        for scope in file.scopes.values():
            unknown = dict_minus(scope.uses, scope.defs)
            unknown = dict_minus(unknown, self.builtins)
            for var in unknown:
                for p in parents(scope):
                    if p.defs.has_key(var):
                        break
                else:
                    if not _importedName(scope, var):
                        file.warning(scope.uses[var], self.unknown, var)

def _first_arg_defaulted(function_node):
    count = len(function_node.argnames)
    if function_node.varargs:
        count -= 1
    if function_node.kwargs:
        count -= 1
    if count > 0 and len(function_node.defaults) == count:
        return 1
    return None

class SelfCheck(Check):
    'Check for simple self parameter'

    selfName = Warning('Report any methods whose first argument is not self',
                       'First argument to method %s (%s) is not in %s')
    selfDefault = Warning('Report a self parameter with a default value',
                          'First argument to method %s (%s) has a default value')

    functionSelf = Warning('Report functions (not methods) with '
                           'arguments named "self"',
                           'Argument to function (%s) is "%s"')

    missingSelf = Warning('Report methods without "self"',
                          'Method %s is missing self parameter')

    def get_options(self, options):
        desc = 'Name of self parameter'
        default = ["self", "this", "s"]
        options.add(Opt(self, 'selfNames', desc, default))

        desc = 'Suspicious self parameters'
        self.selfSuspicious = ["self"]
        options.add(Opt(self, 'selfSuspicious', desc, self.selfSuspicious))

    def check(self, file, unused_checker):
        self.selfNames      = _str_value(self.selfNames)
        self.selfSuspicious = _str_value(self.selfSuspicious)

        for node, scope in file.function_scopes():
            args = node.argnames
            name = getattr(scope.node, 'name', 'lambda')
            if _is_method(scope):
                if not args:
                    file.warning(scope.node, self.missingSelf, name)
                else:
                    if args[0] not in self.selfNames:
                        file.warning(scope.node, self.selfName,
                                     name, args[0], `self.selfNames`)
                    if _first_arg_defaulted(scope.node):
                        file.warning(scope.node, self.selfDefault,
                                     name, args[0])
            else:
                for arg in args:
                    if arg in self.selfSuspicious:
                        file.warning(scope.defs[arg], self.functionSelf,
                                     name, arg)

class UnpackCheck(Check):
    'Mark all unpacked variables as used'

    def __init__(self):
        self.unpackedUsed = None

    def get_options(self, options):
        desc = 'Do not treat variables used in tuple assignment as used'
        options.add(BoolOpt(self, 'unpackedUsed', desc, 1))

    def check(self, file, unused_checker):
        if not self.unpackedUsed:
            return

        class Visitor:
            def visitAssTuple(self, node):
                for c in node.getChildNodes():
                    for n in parents(c):
                        try:
                            file.scopes[n].uses[c.name] = node
                            break
                        except (KeyError, AttributeError):
                            pass
            visitAssList = visitAssTuple

        # local args unpacked on the `def' line are used, too
        for scope_node, scope in file.function_scopes():
            for arg in type_filter(scope_node.argnames, tuple):
                for unpacked in ast.flatten(arg):
                    scope.uses[unpacked] = scope.uses.get(unpacked, scope_node)

        if file.root_scope:
            walk(file.root_scope.node, Visitor())

def intersect2(a, b):
    return [i for i in a if i in b]

def intersect(items):
    result = items[0]
    for item in items[1:]:
        result = intersect2(result, item)
    return result

class UsedBeforeSetCheck(Check):

    usedBeforeDefined = \
          Warning('Report variables that may be used before they are defined',
                  'The local %s may be used before defined')

    def check(self, file, unused_checker):
        class Visitor(BaseVisitor):
            def __init__(self, defines = None, uses = None):
                # list of vars defined
                self.defines = []
                # name->node vars used before defined
                self.uses = {}
                if defines is not None:
                    self.defines = defines[:]
                if uses is not None:
                    self.uses = uses.copy()
            def visitFunction(self, n):
                self.defines.append(n.name)

            visitClass = visitFunction
            visitAssName = visitFunction

            def visitGlobal(self, node):
                for name in node.names:
                    self.defines.append(name)

            def visitListComp(self, n):
                # visit qualifiers before expression
                children = ast.flatten_nodes(n.quals) + [n.expr]
                for c in children:
                    self.visit(c)

            def visitName(self, n):
                if n.name not in self.defines:
                    self.uses[n.name] = self.uses.get(n.name, n)

            def visitIf(self, n):
                if not n.else_:
                    return
                visits = []
                for test, code in n.tests:
                    visits.append(walk(code, Visitor(self.defines, self.uses)))
                visits.append(walk(n.else_, Visitor(self.defines, self.uses)))
                # compute the intersection of defines
                self.defines = intersect([v.defines for v in visits])
                # compute the union of uses, perserving first occurances
                union = {}
                visits.reverse()
                for v in visits:
                    union.update(v.uses)
                union.update(self.uses)
                self.uses = union

        for node, scope in file.function_scopes():
            predefined = ast.flatten(scope.params) + scope.imports.keys()
            visitor = walk(node.code, Visitor(predefined))
            usedBefore = dict_intersect(visitor.uses, scope.defs)
            for name, use_node in usedBefore.items():
                file.warning(use_node, self.usedBeforeDefined, name)
<|endoftext|>


<commit_before>#! /usr/bin/env python
# -*- coding: iso-8859-1 -*-
# vi:ts=4:et
# $Id$

import time
import pycurl

## Callback function invoked when progress information is updated
def progress(download_t, download_d, upload_t, upload_d):
    print "Total to download %d bytes, have %d bytes so far" % \
          (download_t, download_d)

url = "http://www.cnn.com"

print "Starting downloading", url
print
f = open("body", "wb")
h = open("header", "wb")
c = pycurl.Curl()
c.setopt(c.URL, url)
c.setopt(c.WRITEDATA, f)
c.setopt(c.NOPROGRESS, 0)
c.setopt(c.PROGRESSFUNCTION, progress)
c.setopt(c.FOLLOWLOCATION, 1)
c.setopt(c.MAXREDIRS, 5)
c.setopt(c.WRITEHEADER, h)
c.setopt(c.OPT_FILETIME, 1)
c.perform()

print
print "Download speed: %.2f bytes/second" % c.getinfo(c.SPEED_DOWNLOAD)
print "Document size: %d bytes" % c.getinfo(c.SIZE_DOWNLOAD)
print "Effective URL:", c.getinfo(c.EFFECTIVE_URL)
print "Content-type:", c.getinfo(c.CONTENT_TYPE)
print "Redirect-time:", c.getinfo(c.REDIRECT_TIME)
print "Redirect-count:", c.getinfo(c.REDIRECT_COUNT)
epoch = c.getinfo(c.INFO_FILETIME)
print "Filetime: %d (%s)" % (epoch, time.ctime(epoch))
print
print "Header is in file 'header', body is in file 'body'"

c.close()
f.close()
h.close()
<commit_msg>Print some more info.<commit_after>#! /usr/bin/env python
# -*- coding: iso-8859-1 -*-
# vi:ts=4:et
# $Id$

import time
import pycurl

## Callback function invoked when progress information is updated
def progress(download_t, download_d, upload_t, upload_d):
    print "Total to download %d bytes, have %d bytes so far" % \
          (download_t, download_d)

url = "http://www.cnn.com"

print "Starting downloading", url
print
f = open("body", "wb")
h = open("header", "wb")
c = pycurl.Curl()
c.setopt(c.URL, url)
c.setopt(c.WRITEDATA, f)
c.setopt(c.NOPROGRESS, 0)
c.setopt(c.PROGRESSFUNCTION, progress)
c.setopt(c.FOLLOWLOCATION, 1)
c.setopt(c.MAXREDIRS, 5)
c.setopt(c.WRITEHEADER, h)
c.setopt(c.OPT_FILETIME, 1)
c.perform()

print
print "HTTP-code:", c.getinfo(c.HTTP_CODE)
print "Total-time:", c.getinfo(c.TOTAL_TIME)
print "Download speed: %.2f bytes/second" % c.getinfo(c.SPEED_DOWNLOAD)
print "Document size: %d bytes" % c.getinfo(c.SIZE_DOWNLOAD)
print "Effective URL:", c.getinfo(c.EFFECTIVE_URL)
print "Content-type:", c.getinfo(c.CONTENT_TYPE)
print "Namelookup-time:", c.getinfo(c.NAMELOOKUP_TIME)
print "Redirect-time:", c.getinfo(c.REDIRECT_TIME)
print "Redirect-count:", c.getinfo(c.REDIRECT_COUNT)
epoch = c.getinfo(c.INFO_FILETIME)
print "Filetime: %d (%s)" % (epoch, time.ctime(epoch))
print
print "Header is in file 'header', body is in file 'body'"

c.close()
f.close()
h.close()
<|endoftext|>


<commit_before># coding=utf-8
import base64
import time
import os

from selenium import webdriver
from selenium.common import exceptions
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

def make_snapshot(
    html_path: str,
    image_name: str,
    pixel_ratio: int = 2,
    delay: int = 2,
    is_remove_html: bool = True,
    browser='Chrome'
):
    if browser == 'Chrome':
        driver = get_chrome()
    elif browser == 'Safari':
        driver = get_safari()
    else:
        raise Exception('Unknown browser!')
    driver.set_script_timeout(delay + 1)

    if not html_path.startswith("http"):
        html_path = 'file://' + os.path.abspath(html_path)
    driver.get(html_path)
    time.sleep(delay)

    ext = image_name.split(".")[1]

    try:
        output = driver.execute_script(__gen_js_code(ext, pixel_ratio, delay))
    except exceptions.TimeoutException:
        pass

    try:
        print(output)
        output = output.split(",")[1]
    except:
        raise

    with open(image_name, "wb") as f:
        f.write(base64.b64decode(output))

    if is_remove_html:
        os.remove(html_path)
    driver.close()
    print("render {}".format(image_name))

def __gen_js_code(file_type: str, pixel_ratio: int, delay: int) -> str:
    script = (
        """
        var ele = document.querySelector('div[_echarts_instance_]');
        var mychart = echarts.getInstanceByDom(ele);
        return mychart.getDataURL(
            {type:'--file-type--', pixelRatio: --pixel-ratio--, excludeComponents: ['toolbox']});
    """.replace(
            "--file-type--", file_type
        )
        .replace("--pixel-ratio--", str(pixel_ratio))
    )
    return script

def get_chrome():
    option = webdriver.ChromeOptions()
    option.add_argument("headless")
    capabilities = DesiredCapabilities.CHROME
    capabilities["loggingPrefs"] = {"browser": "ALL"}
    return webdriver.Chrome(
        options=option,
        desired_capabilities=capabilities)

def get_safari():
    return webdriver.Safari(executable_path='/usr/bin/safaridriver')
<commit_msg>:bug: fix html file removing bug and enable google chrome as a driver on mac os<commit_after># coding=utf-8
import base64
import time
import sys
import os

from selenium import webdriver
from selenium.common import exceptions
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

def make_snapshot(
    html_path: str,
    image_name: str,
    pixel_ratio: int = 2,
    delay: int = 2,
    is_remove_html: bool = True,
    browser='Chrome'
):
    if browser == 'Chrome':
        driver = get_chrome()
    elif browser == 'Safari':
        driver = get_safari()
    else:
        raise Exception('Unknown browser!')
    driver.set_script_timeout(delay + 1)

    if not html_path.startswith("http"):
        html_path = 'file://' + os.path.abspath(html_path)
    driver.get(html_path)
    time.sleep(delay)

    ext = image_name.split(".")[1]

    try:
        output = driver.execute_script(__gen_js_code(ext, pixel_ratio, delay))
    except exceptions.TimeoutException:
        pass

    try:
        output = output.split(",")[1]
    except:
        raise

    with open(image_name, "wb") as f:
        f.write(base64.b64decode(output))

    if is_remove_html and not html_path.startswith("http"):
        os.remove(html_path[7:])
    driver.close()
    print("render {}".format(image_name))

def __gen_js_code(file_type: str, pixel_ratio: int, delay: int) -> str:
    script = (
        """
        var ele = document.querySelector('div[_echarts_instance_]');
        var mychart = echarts.getInstanceByDom(ele);
        return mychart.getDataURL(
            {type:'--file-type--', pixelRatio: --pixel-ratio--, excludeComponents: ['toolbox']});
    """.replace(
            "--file-type--", file_type
        )
        .replace("--pixel-ratio--", str(pixel_ratio))
    )
    return script

def get_chrome():
    option = webdriver.ChromeOptions()
    option.add_argument("headless")
    if sys.platform == 'darwin':
        option.binary_location = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
    capabilities = DesiredCapabilities.CHROME
    capabilities["loggingPrefs"] = {"browser": "ALL"}
    return webdriver.Chrome(
        options=option,
        desired_capabilities=capabilities)

def get_safari():
    return webdriver.Safari(executable_path='/usr/bin/safaridriver')
<|endoftext|>