Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
R
robo-dividend
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wenwen.tang
robo-dividend
Commits
228349ea
Commit
228349ea
authored
Nov 10, 2022
by
jichao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
依赖注入实现中
parent
4175cece
Hide whitespace changes
Inline
Side-by-side
Showing
17 changed files
with
152 additions
and
110 deletions
+152
-110
api.py
api.py
+13
-0
config.yml
config.yml
+47
-46
robo_base_datum.py
datas/datum/robo_base_datum.py
+1
-1
impl.py
datas/impl.py
+16
-0
api.py
datas/navs/api.py
+2
-2
robo_exrate.py
datas/navs/robo_exrate.py
+2
-2
robo_fund_navs.py
datas/navs/robo_fund_navs.py
+2
-2
__env_config.py
framework/__env_config.py
+0
-0
__init__.py
framework/__init__.py
+4
-2
__logger.py
framework/__logger.py
+2
-2
base.py
framework/base.py
+0
-0
date_utils.py
framework/date_utils.py
+0
-0
datebase.py
framework/datebase.py
+15
-2
injectable.py
framework/injectable.py
+26
-47
fund_optimize.py
fund_pool/fund_optimize.py
+1
-1
service.py
fund_pool/service.py
+12
-0
main.py
main.py
+9
-3
No files found.
api.py
0 → 100644
View file @
228349ea
from
abc
import
ABCMeta
,
abstractmethod
class
BaseBean
(
metaclass
=
ABCMeta
):
@
abstractmethod
def
do_something
(
self
):
pass
class
ServiceBean
(
metaclass
=
ABCMeta
):
@
abstractmethod
def
do_service
(
self
):
pass
config.yml
View file @
228349ea
database
:
host
:
${MYSQL_HOST:127.0.0.1}
port
:
${MYSQL_PORT:3306}
user
:
${MYSQL_USER:root}
password
:
${MYSQL_PWD:123456}
dbname
:
${MYSQL_DBNAME:jftech_robo}
database_from
:
host
:
${MYSQL_HOST:127.0.0.1}
port
:
${MYSQL_PORT:3306}
user
:
${MYSQL_USER:root}
password
:
${MYSQL_PWD:123456}
dbname
:
${MYSQL_DBNAME:robo_pmpt}
email
:
server
:
smtphz.qiye.163.com
user
:
jft-ra@thizgroup.com
password
:
5dbb#30ec6d3
logger
:
version
:
1
use
:
${LOG_NAME:root}
formatters
:
brief
:
format
:
"
%(asctime)s
-
%(levelname)s
-
%(message)s"
simple
:
format
:
"
%(asctime)s
-
%(filename)s
-
%(levelname)s
-
%(message)s"
handlers
:
console
:
class
:
logging.StreamHandler
formatter
:
simple
framework
:
database
:
host
:
${MYSQL_HOST:127.0.0.1}
port
:
${MYSQL_PORT:3306}
user
:
${MYSQL_USER:root}
password
:
${MYSQL_PWD:123456}
dbname
:
${MYSQL_DBNAME:jftech_robo}
database_from
:
host
:
${MYSQL_HOST:127.0.0.1}
port
:
${MYSQL_PORT:3306}
user
:
${MYSQL_USER:root}
password
:
${MYSQL_PWD:123456}
dbname
:
${MYSQL_DBNAME:robo_pmpt}
email
:
server
:
smtphz.qiye.163.com
user
:
jft-ra@thizgroup.com
password
:
5dbb#30ec6d3
logger
:
version
:
1
use
:
${LOG_NAME:root}
formatters
:
brief
:
format
:
"
%(asctime)s
-
%(levelname)s
-
%(message)s"
simple
:
format
:
"
%(asctime)s
-
%(filename)s
-
%(levelname)s
-
%(message)s"
handlers
:
console
:
class
:
logging.StreamHandler
formatter
:
simple
level
:
INFO
stream
:
ext://sys.stdout
file
:
class
:
logging.handlers.TimedRotatingFileHandler
level
:
INFO
formatter
:
brief
filename
:
logs/info.log
interval
:
1
backupCount
:
30
encoding
:
utf8
when
:
D
loggers
:
prod
:
handlers
:
[
console
,
file
]
level
:
INFO
propagate
:
0
root
:
level
:
INFO
stream
:
ext://sys.stdout
file
:
class
:
logging.handlers.TimedRotatingFileHandler
level
:
INFO
formatter
:
brief
filename
:
logs/info.log
interval
:
1
backupCount
:
30
encoding
:
utf8
when
:
D
loggers
:
prod
:
handlers
:
[
console
,
file
]
level
:
INFO
propagate
:
0
root
:
level
:
INFO
handlers
:
[
console
]
handlers
:
[
console
]
datas
:
navs
:
exrate
:
...
...
datas/datum/robo_base_datum.py
View file @
228349ea
from
utils
import
read
,
write
,
config
,
format_date
,
parse_date
,
where
from
framework
import
read
,
write
,
config
,
format_date
,
parse_date
,
where
import
json
from
datetime
import
datetime
from
datas.datum.enums
import
DatumType
...
...
datas/impl.py
0 → 100644
View file @
228349ea
from
api
import
BaseBean
from
framework
import
component
@
component
(
bean_name
=
"one"
)
class
OneImplBean
(
BaseBean
):
def
do_something
(
self
):
print
(
"one bean"
)
@
component
(
bean_name
=
"two"
)
class
TwoImplBean
(
BaseBean
):
def
do_something
(
self
):
print
(
"two bean"
)
\ No newline at end of file
datas/navs/api.py
View file @
228349ea
import
pandas
as
_pd
from
datas.navs
import
robo_exrate
as
_re
,
robo_fund_navs
as
_navs
from
datas
import
datum
as
_datum
,
DatumType
from
utils
import
config
,
to_bool
from
framework
import
config
,
to_bool
from
datetime
import
timedelta
navs_config
=
config
[
'datas'
][
'navs'
]
if
'datas'
in
config
and
'navs'
in
config
[
'datas'
]
else
{}
...
...
@@ -27,6 +27,6 @@ def get_navs(fund_id=None, min_date=None, max_date=None):
if
__name__
==
'__main__'
:
from
utils
import
parse_date
from
framework
import
parse_date
print
(
get_navs
(
min_date
=
parse_date
(
'2022-11-01'
)))
datas/navs/robo_exrate.py
View file @
228349ea
from
utils
import
read
,
where
,
format_date
from
framework
import
read
,
where
,
format_date
@
read
...
...
@@ -21,6 +21,6 @@ def get_exrate(ticker, date):
if
__name__
==
'__main__'
:
from
utils
import
parse_date
from
framework
import
parse_date
print
(
get_exrate
(
date
=
parse_date
(
'2022-11-01'
),
ticker
=
'EURUSD BGN Curncy'
))
datas/navs/robo_fund_navs.py
View file @
228349ea
from
utils
import
read
,
where
,
format_date
from
framework
import
read
,
where
,
format_date
@
read
...
...
@@ -16,6 +16,6 @@ def get_navs(fund_id=None, min_date=None, max_date=None):
if
__name__
==
'__main__'
:
from
utils
import
parse_date
from
framework
import
parse_date
navs
=
get_navs
(
fund_id
=
1
,
min_date
=
parse_date
(
'2022-11-01'
))
print
(
navs
)
utils
/__env_config.py
→
framework
/__env_config.py
View file @
228349ea
File moved
utils
/__init__.py
→
framework
/__init__.py
View file @
228349ea
...
...
@@ -3,6 +3,8 @@ from .base import *
from
.datebase
import
read
,
write
,
transaction
,
where
from
.__env_config
import
config
,
get_config
from
.__logger
import
build_logger
,
logger
from
.injectable
import
component
from
.injectable
import
component
,
autowired
,
get_instance
,
init_injectable
as
_init_injectable
del
injectable
,
__logger
,
__env_config
,
datebase
,
base
,
date_utils
_init_injectable
()
del
injectable
,
__logger
,
__env_config
,
datebase
,
base
,
date_utils
,
_init_injectable
utils
/__logger.py
→
framework
/__logger.py
View file @
228349ea
...
...
@@ -14,5 +14,5 @@ def build_logger(config, name='root'):
return
getLogger
(
name
)
if
'
logger'
in
config
:
logger
=
build_logger
(
config
[
'
logger'
],
name
=
config
[
'logger'
][
'use'
])
if
'
framework'
in
config
and
'logger'
in
config
[
'framework'
]
:
logger
=
build_logger
(
config
[
'
framework'
][
'logger'
],
name
=
config
[
'framework'
]
[
'logger'
][
'use'
])
utils
/base.py
→
framework
/base.py
View file @
228349ea
File moved
utils
/date_utils.py
→
framework
/date_utils.py
View file @
228349ea
File moved
utils
/datebase.py
→
framework
/datebase.py
View file @
228349ea
...
...
@@ -2,14 +2,27 @@ import functools
import
pymysql
import
threading
from
pymysql.cursors
import
DictCursor
from
.__env_config
import
config
as
default
_config
from
.__env_config
import
config
as
global
_config
from
.date_utils
import
format_date
,
datetime
from
enum
import
Enum
_CONFIG
=
global_config
[
'framework'
][
'database'
]
if
'framework'
in
global_config
and
'database'
in
global_config
[
'framework'
]
else
None
class
DatabaseError
(
Exception
):
def
__init__
(
self
,
msg
):
self
.
__msg
=
msg
def
__str__
(
self
):
return
self
.
__msg
class
Database
:
def
__init__
(
self
,
config
):
self
.
config
=
config
or
default_config
[
'database'
]
self
.
_config
=
config
or
_CONFIG
if
self
.
_config
is
None
:
raise
DatabaseError
(
"database config is not found."
)
def
__enter__
(
self
):
port
=
3306
...
...
utils
/injectable.py
→
framework
/injectable.py
View file @
228349ea
import
abc
import
inspect
import
functools
import
os
,
sys
from
importlib
import
import_module
from
inspect
import
signature
,
Parameter
from
functools
import
partial
,
wraps
from
typing
import
List
,
get_origin
,
get_args
from
types
import
GenericAlias
from
framework.base
import
get_project_path
__COMPONENT_CLASS
=
[]
__NAME_COMPONENT
=
{}
...
...
@@ -19,7 +21,7 @@ class InjectableError(Exception):
def
component
(
cls
=
None
,
bean_name
=
None
):
if
cls
is
None
:
return
functools
.
partial
(
component
,
bean_name
=
bean_name
)
return
partial
(
component
,
bean_name
=
bean_name
)
__COMPONENT_CLASS
.
append
(
cls
)
if
bean_name
:
...
...
@@ -29,19 +31,23 @@ def component(cls=None, bean_name=None):
return
cls
def
autowired
(
func
=
None
):
def
autowired
(
func
=
None
,
names
=
None
):
if
func
is
None
:
return
functools
.
partial
(
autowired
)
return
partial
(
autowired
,
names
=
names
)
@
functools
.
wraps
(
func
)
@
wraps
(
func
)
def
wrap
(
*
args
,
**
kwargs
):
self_type
=
type
(
args
[
0
])
if
self_type
in
__COMPONENT_CLASS
and
self_type
not
in
__COMPONENT_INSTANCE
:
__COMPONENT_INSTANCE
[
self_type
]
=
args
[
0
]
for
p_name
,
p_type
in
inspect
.
signature
(
func
)
.
parameters
.
items
():
if
p_name
==
'self'
or
p_type
==
inspect
.
Parameter
.
empty
:
if
func
.
__name__
==
'__init__'
:
self_type
=
type
(
args
[
0
])
if
self_type
in
__COMPONENT_CLASS
and
self_type
not
in
__COMPONENT_INSTANCE
:
__COMPONENT_INSTANCE
[
self_type
]
=
args
[
0
]
for
p_name
,
p_type
in
signature
(
func
)
.
parameters
.
items
():
if
p_name
==
'self'
or
p_type
==
Parameter
.
empty
or
p_name
in
kwargs
:
continue
if
get_origin
(
p_type
.
annotation
)
is
list
:
if
names
is
not
None
and
p_name
in
names
:
if
names
[
p_name
]
in
__NAME_COMPONENT
:
kwargs
[
p_name
]
=
get_instance
(
__NAME_COMPONENT
[
names
[
p_name
]])
elif
get_origin
(
p_type
.
annotation
)
is
list
:
inject_types
=
get_args
(
p_type
.
annotation
)
if
len
(
inject_types
)
>
0
:
instances
=
[
get_instance
(
x
)
for
x
in
__COMPONENT_CLASS
if
issubclass
(
x
,
inject_types
)]
...
...
@@ -64,37 +70,10 @@ def get_instance(t):
return
__COMPONENT_INSTANCE
[
t
]
class
BaseBean
(
metaclass
=
abc
.
ABCMeta
):
@
abc
.
abstractmethod
def
do_something
(
self
):
pass
@
component
(
bean_name
=
"one"
)
class
OneImplBean
(
BaseBean
):
def
do_something
(
self
):
print
(
"one bean"
)
@
component
(
bean_name
=
"two"
)
class
TwoImplBean
(
BaseBean
):
def
do_something
(
self
):
print
(
"two bean"
)
class
ServiceBean
:
@
autowired
def
__init__
(
self
,
base
:
BaseBean
=
None
,
bases
:
List
[
BaseBean
]
=
None
):
base
.
do_something
()
for
b
in
bases
:
b
.
do_something
()
print
(
__COMPONENT_CLASS
,
__NAME_COMPONENT
)
if
__name__
==
'__main__'
:
ServiceBean
()
def
init_injectable
(
path
=
get_project_path
()):
for
f
in
os
.
listdir
(
path
):
if
os
.
path
.
isdir
(
f
)
and
os
.
path
.
exists
(
os
.
path
.
join
(
f
,
'__init__.py'
)):
init_injectable
(
path
=
os
.
path
.
join
(
path
,
f
))
if
f
.
endswith
(
'.py'
)
and
f
!=
'__init__.py'
:
py
=
os
.
path
.
relpath
(
os
.
path
.
join
(
path
,
f
),
get_project_path
())[:
-
3
]
import_module
(
'.'
.
join
(
py
.
split
(
os
.
path
.
sep
)))
fund_pool/fund_optimize.py
View file @
228349ea
import
pandas
as
pd
from
utils
import
filter_weekend
,
config
,
dict_remove
from
framework
import
filter_weekend
,
config
,
dict_remove
from
datas
import
navs
from
dateutil.relativedelta
import
relativedelta
from
empyrical
import
sortino_ratio
...
...
fund_pool/service.py
0 → 100644
View file @
228349ea
from
api
import
BaseBean
,
ServiceBean
from
framework
import
autowired
,
component
@
component
class
ServiceImpl
(
ServiceBean
):
@
autowired
def
__init__
(
self
,
base
:
BaseBean
=
None
):
self
.
_base
=
base
def
do_service
(
self
):
self
.
_base
.
do_something
()
\ No newline at end of file
main.py
View file @
228349ea
from
utils
import
logger
from
datas
import
datum
from
framework
import
logger
,
autowired
from
api
import
ServiceBean
@
autowired
def
test
(
service
:
ServiceBean
=
None
):
service
.
do_service
()
if
__name__
==
'__main__'
:
logger
.
info
(
dir
())
logger
.
info
(
datum
.
get_fund_datums
()
)
test
(
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment