Skip to content

Commit

Permalink
python2 unicode compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
EricWang committed Apr 1, 2017
1 parent cdce623 commit 5266ead
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 43 deletions.
2 changes: 1 addition & 1 deletion rqalpha/api/api_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ def get_dividend(order_book_id, start_date, adjusted=True):
start_date = to_date(start_date)
if start_date > dt:
raise RQInvalidArgument(
_('in get_dividend, start_date {} is later than the previous test day {}').format(
_(u"in get_dividend, start_date {} is later than the previous test day {}").format(
start_date, dt
))
order_book_id = assure_order_book_id(order_book_id)
Expand Down
8 changes: 4 additions & 4 deletions rqalpha/api/api_stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def order_shares(id_or_ins, amount, style=MarketOrder()):
# 如果下单量为0,则认为其并没有发单,则直接返回None
return None
if not isinstance(style, OrderStyle):
raise RQInvalidArgument(_('style should be OrderStyle'))
raise RQInvalidArgument(_(u"style should be OrderStyle"))
if isinstance(style, LimitOrder):
if style.get_limit_price() <= 0:
raise RQInvalidArgument(_(u"Limit order price should be positive"))
Expand Down Expand Up @@ -244,7 +244,7 @@ def order_value(id_or_ins, cash_amount, style=MarketOrder()):
# :return: A unique order id.
# :rtype: int
if not isinstance(style, OrderStyle):
raise RQInvalidArgument(_('style should be OrderStyle'))
raise RQInvalidArgument(_(u"style should be OrderStyle"))
if isinstance(style, LimitOrder):
if style.get_limit_price() <= 0:
raise RQInvalidArgument(_(u"Limit order price should be positive"))
Expand Down Expand Up @@ -321,7 +321,7 @@ def order_percent(id_or_ins, percent, style=MarketOrder()):
# :return: A unique order id.
# :rtype: int
if percent < -1 or percent > 1:
raise RQInvalidArgument(_('percent should between -1 and 1'))
raise RQInvalidArgument(_(u"percent should between -1 and 1"))

account = Environment.get_instance().portfolio.accounts[ACCOUNT_TYPE.STOCK]
return order_value(id_or_ins, account.total_value * percent, style)
Expand Down Expand Up @@ -433,7 +433,7 @@ def order_target_percent(id_or_ins, percent, style=MarketOrder()):
# :return: A unique order id.
# :rtype: int
if percent < 0 or percent > 1:
raise RQInvalidArgument(_('percent should between 0 and 1'))
raise RQInvalidArgument(_(u"percent should between 0 and 1"))
order_book_id = assure_stock_order_book_id(id_or_ins)

account = Environment.get_instance().portfolio.accounts[ACCOUNT_TYPE.STOCK]
Expand Down
8 changes: 4 additions & 4 deletions rqalpha/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def _adjust_start_date(config, data_proxy):
config.base.end_date = min(end, config.base.end_date)
config.base.trading_calendar = data_proxy.get_trading_dates(config.base.start_date, config.base.end_date)
if len(config.base.trading_calendar) == 0:
raise patch_user_exc(ValueError(_('There is no trading day between {start_date} and {end_date}.').format(
raise patch_user_exc(ValueError(_(u"There is no trading day between {start_date} and {end_date}.").format(
start_date=origin_start_date, end_date=origin_end_date)))
config.base.start_date = config.base.trading_calendar[0].date()
config.base.end_date = config.base.trading_calendar[-1].date()
Expand All @@ -77,7 +77,7 @@ def _validate_benchmark(config, data_proxy):
return
instrument = data_proxy.instruments(benchmark)
if instrument is None:
raise patch_user_exc(ValueError(_('invalid benchmark {}').format(benchmark)))
raise patch_user_exc(ValueError(_(u"invalid benchmark {}").format(benchmark)))

if instrument.order_book_id == "000300.XSHG":
# 000300.XSHG 数据进行了补齐,因此认为只要benchmark设置了000300.XSHG,就存在数据,不受限于上市日期。
Expand Down Expand Up @@ -152,7 +152,7 @@ def update_bundle(data_bundle_path=None, locale="zh_Hans_CN", confirm=True):
while True:
url = 'http://7xjci3.com1.z0.glb.clouddn.com/bundles_v2/rqbundle_%04d%02d%02d.tar.bz2' % (
day.year, day.month, day.day)
six.print_(_('try {} ...').format(url))
six.print_(_(u"try {} ...").format(url))
r = requests.get(url, stream=True)
if r.status_code != 200:
day = day - datetime.timedelta(days=1)
Expand All @@ -161,7 +161,7 @@ def update_bundle(data_bundle_path=None, locale="zh_Hans_CN", confirm=True):
out = open(tmp, 'wb')
total_length = int(r.headers.get('content-length'))

with click.progressbar(length=total_length, label=_('downloading ...')) as bar:
with click.progressbar(length=total_length, label=_(u"downloading ...")) as bar:
for data in r.iter_content(chunk_size=8192):
bar.update(len(data))
out.write(data)
Expand Down
2 changes: 1 addition & 1 deletion rqalpha/mod/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def set_env(self, environment):
lib_name = "rqalpha.mod.rqalpha_mod_" + mod_name
else:
lib_name = "rqalpha_mod_" + mod_name
system_log.debug(_('loading mod {}').format(lib_name))
system_log.debug(_(u"loading mod {}").format(lib_name))
mod_module = import_mod(lib_name)
if mod_module is None:
del self._mod_list[idx]
Expand Down
2 changes: 1 addition & 1 deletion rqalpha/mod/rqalpha_mod_sys_risk/is_trading_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def can_submit_order(self, account, order):
return False

if self._env.data_proxy.is_suspended(order.order_book_id, self._env.trading_dt):
order.mark_rejected(_('security {order_book_id} is suspended on {date}').format(
order.mark_rejected(_(u"security {order_book_id} is suspended on {date}").format(
order_book_id=order.order_book_id,
date=self._env.trading_dt
))
Expand Down
8 changes: 4 additions & 4 deletions rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ def match(self, open_orders):
volume_limit = (volume_limit // round_lot) * round_lot
if volume_limit <= 0:
if order.type == ORDER_TYPE.MARKET:
reason = _('Order Cancelled: market order {order_book_id} volume {order_volume}'
' due to volume limit').format(
reason = _(u"Order Cancelled: market order {order_book_id} volume {order_volume}"
u" due to volume limit").format(
order_book_id=order.order_book_id,
order_volume=order.quantity
)
Expand Down Expand Up @@ -149,8 +149,8 @@ def match(self, open_orders):

if order.type == ORDER_TYPE.MARKET and order.unfilled_quantity != 0:
reason = _(
"Order Cancelled: market order {order_book_id} volume {order_volume} is"
" larger than 25 percent of current bar volume, fill {filled_volume} actually"
u"Order Cancelled: market order {order_book_id} volume {order_volume} is"
u" larger than 25 percent of current bar volume, fill {filled_volume} actually"
).format(
order_book_id=order.order_book_id,
order_volume=order.quantity,
Expand Down
46 changes: 23 additions & 23 deletions rqalpha/utils/arg_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def is_instance_of(self, types):
def check_is_instance_of(func_name, value):
if not isinstance(value, types):
raise RQInvalidArgument(
_('function {}: invalid {} argument, expect a value of type {}, got {} (type: {})').format(
_(u"function {}: invalid {} argument, expect a value of type {}, got {} (type: {})").format(
func_name, self._arg_name, types, value, type(value)
))

Expand All @@ -54,22 +54,22 @@ def check_is_instance_of(func_name, value):

def raise_not_valid_instrument_error(self, func_name, arg_name, value):
raise RQInvalidArgument(
_('function {}: invalid {} argument, expect a valid instrument/order_book_id/symbol, '
'got {} (type: {})').format(
_(u"function {}: invalid {} argument, expect a valid instrument/order_book_id/symbol, "
u"got {} (type: {})").format(
func_name, self._arg_name, value, type(value)
))

def raise_not_valid_stock_error(self, func_name, arg_name, value):
raise RQInvalidArgument(
_('function {}: invalid {} argument, expect a valid stock instrument/order_book_id/symbol, '
'got {} (type: {})').format(
_(u"function {}: invalid {} argument, expect a valid stock instrument/order_book_id/symbol, "
u"got {} (type: {})").format(
func_name, self._arg_name, value, type(value)
))

def raise_not_valid_future_error(self, func_name, arg_name, value):
raise RQInvalidArgument(
_('function {}: invalid {} argument, expect a valid future instrument/order_book_id/symbol, '
'got {} (type: {})').format(
_(u"function {}: invalid {} argument, expect a valid future instrument/order_book_id/symbol, "
u"got {} (type: {})").format(
func_name, self._arg_name, value, type(value)
))

Expand Down Expand Up @@ -152,7 +152,7 @@ def _is_number(self, func_name, value):
v = float(value)
except ValueError:
raise RQInvalidArgument(
_('function {}: invalid {} argument, expect a number, got {} (type: {})').format(
_(u"function {}: invalid {} argument, expect a number, got {} (type: {})").format(
func_name, self._arg_name, value, type(value))
)

Expand All @@ -167,7 +167,7 @@ def check_is_in(func_name, value):

if value not in valid_values:
raise RQInvalidArgument(
_('function {}: invalid {} argument, valid: {}, got {} (type: {})').format(
_(u"function {}: invalid {} argument, valid: {}, got {} (type: {})").format(
func_name, self._arg_name, repr(valid_values), value, type(value))
)
return
Expand All @@ -182,7 +182,7 @@ def check_are_valid_fields(func_name, fields):
if isinstance(fields, six.string_types):
if fields not in valid_fields:
raise RQInvalidArgument(
_('function {}: invalid {} argument, valid fields are {}, got {} (type: {})').format(
_(u"function {}: invalid {} argument, valid fields are {}, got {} (type: {})").format(
func_name, self._arg_name, repr(valid_fields), fields, type(fields)
))
return
Expand All @@ -194,12 +194,12 @@ def check_are_valid_fields(func_name, fields):
invalid_fields = [field for field in fields if field not in valid_fields]
if invalid_fields:
raise RQInvalidArgument(
_('function {}: invalid field {}, valid fields are {}, got {} (type: {})').format(
_(u"function {}: invalid field {}, valid fields are {}, got {} (type: {})").format(
func_name, invalid_fields, repr(valid_fields), fields, type(fields)
))

raise RQInvalidArgument(
_('function {}: invalid {} argument, expect a string or a list of string, got {} (type: {})').format(
_(u"function {}: invalid {} argument, expect a string or a list of string, got {} (type: {})").format(
func_name, self._arg_name, repr(fields), type(fields)
))
self._rules.append(check_are_valid_fields)
Expand All @@ -216,7 +216,7 @@ def _are_valid_instruments(self, func_name, values):
return

raise RQInvalidArgument(
_('function {}: invalid {} argument, expect a string or a list of string, got {} (type: {})').format(
_(u"function {}: invalid {} argument, expect a string or a list of string, got {} (type: {})").format(
func_name, self._arg_name, repr(values), type(values)
))

Expand All @@ -235,12 +235,12 @@ def check_is_valid_date(func_name, value):
v = parse_date(value)
except ValueError:
raise RQInvalidArgument(
_('function {}: invalid {} argument, expect a valid date, got {} (type: {})').format(
_(u"function {}: invalid {} argument, expect a valid date, got {} (type: {})").format(
func_name, self._arg_name, value, type(value)
))

raise RQInvalidArgument(
_('function {}: invalid {} argument, expect a valid date, got {} (type: {})').format(
_(u"function {}: invalid {} argument, expect a valid date, got {} (type: {})").format(
func_name, self._arg_name, value, type(value)
))

Expand All @@ -251,7 +251,7 @@ def is_greater_or_equal_than(self, low):
def check_greater_or_equal_than(func_name, value):
if value < low:
raise RQInvalidArgument(
_('function {}: invalid {} argument, expect a value >= {}, got {} (type: {})').format(
_(u"function {}: invalid {} argument, expect a value >= {}, got {} (type: {})").format(
func_name, self._arg_name, low, value, type(value)
))
self._rules.append(check_greater_or_equal_than)
Expand All @@ -261,7 +261,7 @@ def is_greater_than(self, low):
def check_greater_than(func_name, value):
if value <= low:
raise RQInvalidArgument(
_('function {}: invalid {} argument, expect a value > {}, got {} (type: {})').format(
_(u"function {}: invalid {} argument, expect a value > {}, got {} (type: {})").format(
func_name, self._arg_name, low, value, type(value)
))
self._rules.append(check_greater_than)
Expand All @@ -271,7 +271,7 @@ def is_less_or_equal_than(self, high):
def check_less_or_equal_than(func_name, value):
if value > high:
raise RQInvalidArgument(
_('function {}: invalid {} argument, expect a value <= {}, got {} (type: {})').format(
_(u"function {}: invalid {} argument, expect a value <= {}, got {} (type: {})").format(
func_name, self._arg_name, high, value, type(value)
))

Expand All @@ -282,7 +282,7 @@ def is_less_than(self, high):
def check_less_than(func_name, value):
if value >= high:
raise RQInvalidArgument(
_('function {}: invalid {} argument, expect a value < {}, got {} (type: {})').format(
_(u"function {}: invalid {} argument, expect a value < {}, got {} (type: {})").format(
func_name, self._arg_name, high, value, type(value)
))

Expand All @@ -300,7 +300,7 @@ def _is_valid_interval(self, func_name, value):
if not valid:
raise RQInvalidArgument(
_(u"function {}: invalid {} argument, interval should be in form of '1d', '3m', '4q', '2y', "
"got {} (type: {})").format(
u"got {} (type: {})").format(
func_name, self.arg_name, value, type(value)
))

Expand All @@ -314,7 +314,7 @@ def _are_valid_query_entities(self, func_name, entities):
if not isinstance(e, InstrumentedAttribute):
raise RQInvalidArgument(
_(u"function {}: invalid {} argument, should be entity like "
"Fundamentals.balance_sheet.total_equity, got {} (type: {})").format(
u"Fundamentals.balance_sheet.total_equity, got {} (type: {})").format(
func_name, self.arg_name, e, type(e)
))

Expand All @@ -323,7 +323,7 @@ def are_valid_query_entities(self):
return self

def _is_valid_frequency(self, func_name, value):
valid = isinstance(value, six.string_types) and value[-1] in ('d', 'm')
valid = isinstance(value, six.string_types) and value[-1] in ("d", "m")
if valid:
try:
valid = int(value[:-1]) > 0
Expand All @@ -333,7 +333,7 @@ def _is_valid_frequency(self, func_name, value):
if not valid:
raise RQInvalidArgument(
_(u"function {}: invalid {} argument, frequency should be in form of "
"'1m', '5m', '1d', got {} (type: {})").format(
u"'1m', '5m', '1d', got {} (type: {})").format(
func_name, self.arg_name, value, type(value)
))

Expand Down
10 changes: 5 additions & 5 deletions rqalpha/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,13 @@ def parse_config(config_args, config_path=None, click_type=True, source_code=Non
extra_config.context_vars = json.loads(to_utf8(extra_config.context_vars))

if base_config.stock_starting_cash < 0:
raise patch_user_exc(ValueError(_('invalid stock starting cash: {}').format(base_config.stock_starting_cash)))
raise patch_user_exc(ValueError(_(u"invalid stock starting cash: {}").format(base_config.stock_starting_cash)))

if base_config.future_starting_cash < 0:
raise patch_user_exc(ValueError(_('invalid future starting cash: {}').format(base_config.future_starting_cash)))
raise patch_user_exc(ValueError(_(u"invalid future starting cash: {}").format(base_config.future_starting_cash)))

if base_config.stock_starting_cash + base_config.future_starting_cash == 0:
raise patch_user_exc(ValueError(_('stock starting cash and future starting cash can not be both 0.')))
raise patch_user_exc(ValueError(_(u"stock starting cash and future starting cash can not be both 0.")))

system_log.level = getattr(logbook, extra_config.log_level.upper(), logbook.NOTSET)
std_log.level = getattr(logbook, extra_config.log_level.upper(), logbook.NOTSET)
Expand Down Expand Up @@ -239,7 +239,7 @@ def parse_user_config_from_code(config, source_code=None):
deep_update(sub_dict, config[sub_key])

except Exception as e:
system_log.error(_('in parse_user_config, exception: {e}').format(e=e))
system_log.error(_(u"in parse_user_config, exception: {e}").format(e=e))
finally:
return config

Expand Down Expand Up @@ -272,4 +272,4 @@ def parse_persist_mode(persist_mode):
elif persist_mode == 'on_crash':
return PERSIST_MODE.ON_CRASH
else:
raise RuntimeError(_('unknown persist mode: {persist_mode}').format(persist_mode=persist_mode))
raise RuntimeError(_(u"unknown persist mode: {persist_mode}").format(persist_mode=persist_mode))

0 comments on commit 5266ead

Please sign in to comment.