当前位置: 首页>>代码示例>>Python>>正文


Python Money.values方法代码示例

本文整理汇总了Python中money.Money.values方法的典型用法代码示例。如果您正苦于以下问题:Python Money.values方法的具体用法?Python Money.values怎么用?Python Money.values使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在money.Money的用法示例。


在下文中一共展示了Money.values方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Player

# 需要导入模块: from money import Money [as 别名]
# 或者: from money.Money import values [as 别名]
class Player(object):
    def __init__(self, player_id=None, name=None, email=None, password=None, created=None, updated=None):
        self.player_id = player_id
        self.name = name
        self.email = email
        self.password = password
        self.created = created
        self.updated = updated
        self.sessions = []
        self.wallet = {}
        self.counter = Counter(player_id=self.player_id)
        self.achievements = []

    def login(self, password):
        if self.password == password:
            current_session = Session(player_id=self.player_id)
            current_session.start_session()
            self.sessions.append(current_session)
            print("Player {} has logged in".format(self.name))
        else:
            print("Password error. Try again.")

    def logout(self):
        self.sessions[-1].finish_session()
        print("Player {} has logged out".format(self.name))

    def give_money(self, code, amount):
        self.wallet[code].amount += amount
        print("{}: + {} {}".format(self.name, code, amount))

    def take_money(self, code, amount):
        self.wallet[code].amount -= amount
        print("{}: - {} {}".format(self.name, code, amount))

    def as_dict(self):
        d = {
            "player_id": self.player_id,
            "type": self.__class__.__name__,
            "name": self.name,
            "email": self.email,
            "password": self.password,
            "created": self.created,
            "updated": self.updated,
            "sessions": [x.as_dict() for x in self.sessions],
            "wallet": [x.as_dict() for x in self.wallet.values()],
            "counter": self.counter.as_dict(),
            "achievements": [x.as_dict() for x in self.achievements]
        }
        return d

    def load(self, file_object):
        object_as_dict = json.load(open(file_object))
        self.player_id = object_as_dict["player_id"]
        self.name = object_as_dict["name"]
        self.email = object_as_dict["email"]
        self.password = object_as_dict["password"]
        self.created = object_as_dict["created"]
        self.updated = object_as_dict["updated"]
        self.sessions = [Session(x['session_id'], x['player_id'], x['start_time'], x['finish_time'], x['created'], x['updated']) for x in object_as_dict['sessions']]
        self.wallet = {x['code']: Money(x['money_id'], x['player_id'], x['code'], x['amount'], x['created'], x['updated']) for x in object_as_dict['wallet']}
        self.counter = Counter()
        self.counter.counter_id = object_as_dict['counter']['counter_id']
        self.counter.player_id = object_as_dict['counter']['player_id']
        self.counter.games = object_as_dict['counter']['games']
        self.counter.win = object_as_dict['counter']['win']
        self.counter.lose = object_as_dict['counter']['lose']
        self.counter.created = object_as_dict['counter']['created']
        self.counter.updated = object_as_dict['counter']['updated']
        self.achievements = [Achievement(x['achievement_id'], x['player_id'], x['name'], x['description'], x['created'], x['updated']) for x in object_as_dict['achievements']]

    def save(self, file_object):
        json.dump(self.as_dict(), open(file_object, 'w'))

    def __str__(self):
        return '{}(name = "{}", email = "{}", password = "{}")'.format(self.__class__.__name__, self.name, self.email, self.password)

    def load_from_db(self, email):
        cur = con.cursor()
        sql_data_email = {
            "email": email
        }
        sql_query = """SELECT id, type, name, email, password, created, updated FROM player WHERE email=%(email)s"""
        cur.execute(sql_query, sql_data_email)
        row = cur.fetchone()
        self.player_id = row[0]
        self.name = row[2]
        self.email = row[3]
        self.password = row[4]
        self.created = row[5]
        self.updated = row[6]
        if row[1] == 'Player':
            self.__class__ = Player
        elif row[1] == 'Admin':
            self.__class__ = Admin
        else:
            self.__class__ = Moder
        self.sessions = Session().load_from_db_player(self.player_id)
        self.wallet = Money().load_from_db_player(self.player_id)
        self.achievements = Achievement().load_from_db_player(self.player_id)
        self.counter = Counter().load_from_db_player(self.player_id)
#.........这里部分代码省略.........
开发者ID:RubashkoM,项目名称:homework,代码行数:103,代码来源:player.py


注:本文中的money.Money.values方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。