Example #1
0
# -*- coding: utf-8 -*-

import os

from PyDemo01 import run_ex_by_flag


# EXAMPLE 01, compare
def ex01():
    tmp_num = 3
    if 2 < tmp_num < 4:
        print 'Chained comparison operators work! \n' * 3


run_ex_by_flag(ex01)


# EXAMPLE 02, enumerate
def ex02():
    some_list = [(1, 2, 'def'), (2, -4, 'ghi'), (3, 6, 'abc')]
    tmp_list = [(val[2], idx, val) for (idx, val) in enumerate(some_list)]
    tmp_list.sort()
    print tmp_list

    my_phrase = ['No', 'one', 'expects', 'the', 'Spanish', 'Inquisition']
    my_dict = {idx: value for idx, value in enumerate(my_phrase)}
    print my_dict


run_ex_by_flag(ex02)
Example #2
0
# -*- coding: utf-8 -*-

import os

from PyDemo01 import run_ex_by_flag

# EXAMPLE 01, compare
def ex01():
    tmp_num = 3
    if 2 < tmp_num < 4:
        print 'Chained comparison operators work! \n' * 3

run_ex_by_flag(ex01)


# EXAMPLE 02, enumerate
def ex02():
    some_list = [(1, 2, 'def'), (2, -4, 'ghi'), (3, 6, 'abc')]
    tmp_list = [(val[2], idx, val) for (idx, val) in enumerate(some_list)]
    tmp_list.sort()
    print tmp_list
     
    my_phrase = ['No', 'one', 'expects', 'the', 'Spanish', 'Inquisition']
    my_dict = {idx : value for idx, value in enumerate(my_phrase)}
    print my_dict

run_ex_by_flag(ex02)


# EXAMPLE 03, subprocess, try catch block, help
def ex03():
Example #3
0
# collections
# EXAMPLE 01, defaultdict
def ex01_01():
    from collections import defaultdict

    tmp_lst = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4),
               ('red', 1)]
    d_dict = defaultdict(list)  # set default value to []
    for k, v in tmp_lst:
        d_dict[k].append(v)

    print sorted(d_dict.iteritems())


run_ex_by_flag(ex01_01)


def ex01_02():
    tmp_lst = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4),
               ('red', 1)]
    d_dict = {}
    for k, v in tmp_lst:
        d_dict.setdefault(k, []).append(v)  # set default value to []

    print sorted(d_dict.items())


run_ex_by_flag(ex01_02)

Example #4
0
from PyDemo01 import run_ex_by_flag


# EXAMPLE 01, Tips
def ex0101():
    my_map = {'k1': 'v1', 'k2': 'v2', 'k3': 'v3'}
    print 'Yes:'
    for k in my_map:
        print 'key:', k

    print 'No:'
    for k in my_map.keys():
        print 'key:', k


run_ex_by_flag(ex0101)


def ex0102():
    def condition_exp_test(cond):
        return 'Yes' if cond else 'No'

    print 'results:', condition_exp_test(False)


run_ex_by_flag(ex0102)


def ex0103():
    # write long string in multiple lines
    my_str = ('this is test1, '
Example #5
0
import os

from PyDemo01 import run_ex_by_flag

# collections
# EXAMPLE 01, defaultdict
def ex01_01():
    from collections import defaultdict
    
    tmp_lst = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
    d_dict = defaultdict(list)  # set default value to []
    for k, v in tmp_lst:
        d_dict[k].append(v)

    print sorted(d_dict.iteritems())
run_ex_by_flag(ex01_01)

def ex01_02():
    tmp_lst = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
    d_dict = {}
    for k, v in tmp_lst:
        d_dict.setdefault(k, []).append(v)  # set default value to []

    print sorted(d_dict.items())
run_ex_by_flag(ex01_02)

def ex01_03():
    from collections import defaultdict
    
    tmp_lst = [('red', 1), ('blue', 2), ('red', 3), ('blue', 4), ('red', 1), ('blue', 4)]
    d_dict = defaultdict(set)  # set default value to set (include distinct value)
Example #6
0
        return in_deco_func

    my_func = deco_func(my_func)  # re-assign
    my_func('test override')

    print '*' * 30, 'case 3'

    @deco_func
    def my_new_func(name):
        print 'New, hello,', name

    my_new_func('test @deco')


run_ex_by_flag(ex01)


# EXAMPLE 02, method override in class
class OverrideTest(object):
    def check(self):
        ''' only be invoked at first time'''
        print 'check Invoked'
        self.check = self.check_post  # re-assign

    def check_post(self):
        print 'check_post Invoked'


def ex02():
    my_test = OverrideTest()
Example #7
0
def ex24():
    def is_even(x):
        return (x % 2) == 0

    print '*' * 20, 'case 1'
    tmp_lst = range(10)
    print type(tmp_lst)
    print tmp_lst
    
    print '*' * 20, 'case 2'
    tmp_gen = xrange(10)
    print type(tmp_gen)

    print [val for val in tmp_gen if val % 2 == 0]
    print [val for val in tmp_gen if is_even(val)]
    print filter(is_even, tmp_gen)

run_ex_by_flag(ex24)


# EXAMPLE 25, generator
def ex2501():
    # 1, yield
    def generate_ints(n):
        for i in xrange(n):
            yield i
     
    my_gen1 = generate_ints(10)
    print 'Type:', type(my_gen1)
    for ele in my_gen1:
        print 'element:', ele

    my_gen2 = generate_ints(3)
    print 'Type:', type(my_gen2)
    print my_gen2.next()
    print my_gen2.next()
    print next(my_gen2)
    try:
        print next(my_gen2)
    except StopIteration, e:
        print e

run_ex_by_flag(ex2501)


def ex2502():
    # 2, pass value by send()
    def counter(max_value):
        i = 0
        while i < max_value:
            val = (yield i)
            if val is not None:
                i = val
            else:
                i += 1
      
    my_gen = counter(10)
    print 'Type:', type(my_gen)
      
    print my_gen.next()
    print my_gen.next()
    print my_gen.send(8)
    print next(my_gen)
    try:
        print next(my_gen)
    except StopIteration, e:
        print e
Example #8
0
            kittens.append(k)
    print kittens
    del kittens[:]

    print '*' * 20, 'case 2'
    kittens = [k for k, v in cats.items() if v < 7]
    print kittens
    del kittens[:]

    print '*' * 20, 'case 3'
    kittens = map(lambda (k, v): k,
                  filter(lambda (k, v): v < 7, cats.iteritems()))
    print kittens


run_ex_by_flag(ex01)


# EXAMPLE 02, file read
def ex02():
    tmp_file_path = os.path.join(os.getcwd(), 'zjunittest.log')

    with open(tmp_file_path, 'r') as f:
        print 'type:', type(f)
        if '__enter__' in dir(f) and '__exit__' in dir(f):
            print 'with block'
        if '__iter__' in dir(f):
            print 'iterator'

        for line in f:
            print line.strip()
Example #9
0
            print'in in_deco_func'
            func(name)

        return in_deco_func

    my_func = deco_func(my_func)  # re-assign
    my_func('test override')

    print '*' * 30, 'case 3'
    @deco_func
    def my_new_func(name):
        print 'New, hello,', name

    my_new_func('test @deco')

run_ex_by_flag(ex01)


# EXAMPLE 02, method override in class
class OverrideTest(object):
 
    def check(self):
        ''' only be invoked at first time'''
        print 'check Invoked'
        self.check = self.check_post  # re-assign
 
    def check_post(self):
        print 'check_post Invoked'

def ex02():
    my_test = OverrideTest()
Example #10
0
import os

from PyDemo01 import run_ex_by_flag

# EXAMPLE 01, Tips
def ex0101():
    my_map = {'k1':'v1', 'k2':'v2', 'k3':'v3'}
    print 'Yes:'
    for k in my_map:
        print 'key:', k

    print 'No:'
    for k in my_map.keys():
        print 'key:', k

run_ex_by_flag(ex0101)

def ex0102():
    def condition_exp_test(cond):
        return 'Yes' if cond else 'No'
    print 'results:', condition_exp_test(False)

run_ex_by_flag(ex0102)

def ex0103():
    # write long string in multiple lines
    my_str = ('this is test1, '
              'this is test2, '
              'this is test3, ' 'this is test4')
    print 'output:', my_str