Finology 大数据金融

通过大数据以量化金融

看如下代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Component
public class FooServiceImpl implements FooService {
@Autowired
private JdbcTemplate jdbcTemplate;

@Override
@Transactional(rollbackFor = RollbackException.class)
public void insertThenRollback() throws RollbackException {
jdbcTemplate.execute("INSERT INTO FOO (BAR) VALUES ('BBB')");
throw new RollbackException();
}

@Override
public void invokeInsertThenRollback() throws RollbackException {
insertThenRollback();
}
}

方法 invokeInsertThenRollback() 调用一个有 @Transactional 注释的方法,这时,事务未生效。

有两种方法可以解决。

方法一,把自身 FooService 注入进来。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Component
public class FooServiceImpl implements FooService {
@Autowired
private JdbcTemplate jdbcTemplate;

@Autowired
private FooService fooService;

@Override
@Transactional(rollbackFor = RollbackException.class)
public void insertThenRollback() throws RollbackException {
jdbcTemplate.execute("INSERT INTO FOO (BAR) VALUES ('BBB')");
throw new RollbackException();
}

@Override
public void invokeInsertThenRollback() throws RollbackException {
// insertThenRollback();
fooService.insertThenRollback();
}
}

方法二,调用 AopContext.currentProxy() 获取代理类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Component
public class FooServiceImpl implements FooService {
@Autowired
private JdbcTemplate jdbcTemplate;

@Override
@Transactional(rollbackFor = RollbackException.class)
public void insertThenRollback() throws RollbackException {
jdbcTemplate.execute("INSERT INTO FOO (BAR) VALUES ('BBB')");
throw new RollbackException();
}

@Override
public void invokeInsertThenRollback() throws RollbackException {
// insertThenRollback();
((FooService) (AopContext.currentProxy())).invokeInsertThenRollback();
}
}

Python 函数具有非常多的参数形态,刚开始接触时很多人不一定了解所有参数的含义。

位置参数

这个很简单吧,跟其他语言都是一样的。

1
2
3
4
5
6
7
8
9
# 可以设置多个位置参数
def stock_information(code, name):
print('code:', code)
print('name:', name)

stock_information('000001.SZ', '平安银行')

code: 000001.SZ
name: 平安银行

默认参数

1
2
3
4
5
6
7
8
9
# 若不知道股票名称时,可以将股票名默认为“股票”
def stock_information(code, name='股票'):
print('code:', code)
print('name:', name)

stock_information('000001.SZ')

code: 000001.SZ
name: 股票

可变参数

可变参数可以是0个到任意个,调用函数时,会自动将可变参数组装成元组。可变参数的形式为 *args

1
2
3
4
5
6
7
8
9
10
11
# 若不知道要存几天的收盘价
def stock_information(code, name='股票', *args):
print('code:', code)
print('name:', name)
print('others:', args)

stock_information('000001.SZ', '平安银行', '1987-12-22', '银行')

code: 000001.SZ
name: 平安银行
others: ('1987-12-22', '银行')

这里需要注意的是,如果是赋值语句:

1
2
3
4
code, *list = '000001.SZ', '平安银行', '1987-12-22', '银行'
print(type(list))

<class 'list'>

这时,list就是一个list类型的变量了。

关键字参数

和上面不同的是,调用函数时,会自动将关键字参数组装成字典。可变参数的形式为 **kws

1
2
3
4
5
6
7
8
9
10
11
# 想要传入其他别的信息,比如公司成立日期、所属行业
def stock_information(code, name ='股票', **kws):
print('code:', code)
print('name:', name)
print('others:', kws)

stock_information('000001.SZ','平安银行', date='1987-12-22', industry='银行' )

code: 000001.SZ
name: 平安银行
others: {'date': '1987-12-22', 'industry': '银行'}

命名关键字参数

用户想要输入的关键字参数,在关键字参数前加分隔符 * ,即*, nkw,命名关键字不能缺少参数名。Python2貌似是不支持的。

1
2
3
4
5
6
7
8
9
10
def stock_information(code, name='股票', *, industry):
print('code:', code)
print('name:', name)
print('industry:', industry)

stock_information('000001.SZ', '平安银行', industry='银行' )

code: 000001.SZ
name: 平安银行
industry: 银行

参数组合

5种参数中的4种可以组合在一起使用,但组合有顺序。

位置参数、默认参数、可变参数、关键字参数

位置参数、默认参数、命名关键字参数、关键字参数

Python的基础数据结构有以下几种

数组list

1
2
3
4
5
[]

list([1, 2, 3])

list(range(5))

元组tuple

1
2
3
4
5
()

tuple((1, 2))

tuple([1, 2, 3])

字典dict

1
{}

{} 是空dict,{‘key’: ‘value’}

1
2
3
4
5
dict(key = 'k', val = 'v')

dict([('key', 'k'), ('val', 'v')])

{'key': 'k', 'val': 'v'}

集合set

1
2
3
4
5
{'a'}

set((1, 2))

set({1, 2})

如果是空集合,是不能写 {} 的,{} 代表空dict。

空集合是 set()

1
2
3
4
5
6
7
8
9
z = zip(['a', 'b'], [1, 2], [3, 4])
list(z)
[('a', 1, 3), ('b', 2, 4)]


z = zip(['a', 'b'], [1, 2])
l = list(z)
dict(l)
{'a': 1, 'b': 2}
0%