{"id":3064,"date":"2026-02-27T08:45:25","date_gmt":"2026-02-27T05:45:25","guid":{"rendered":"http:\/\/csnotes.ru\/?p=3064"},"modified":"2026-02-27T08:45:26","modified_gmt":"2026-02-27T05:45:26","slug":"%d0%bf%d0%b0%d1%82%d1%82%d0%b5%d1%80%d0%bd-simple-factory","status":"publish","type":"post","link":"https:\/\/csnotes.ru\/?p=3064","title":{"rendered":"\u041f\u0430\u0442\u0442\u0435\u0440\u043d Simple Factory"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"\"\"\u041f\u0440\u043e\u0441\u0442\u0430\u044f \u0444\u0430\u0431\u0440\u0438\u043a\u0430 - \u044d\u0442\u043e \u043f\u0430\u0442\u0442\u0435\u0440\u043d, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0438\u0439 \u0437\u0430\u0434\u0430\u0442\u044c \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u043d\u044b\u0435\n\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u044b \u043a\u043b\u0430\u0441\u0441\u0430 \u0437\u0430 \u0441\u0447\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043a\u043b\u0430\u0441\u0441\u0430.\"\"\"\n\nimport re\n\n\nclass Money:\n    def __init__(self, dollars, cents):\n        self.dollars = dollars\n        self.cents = cents\n\n    @classmethod\n    def from_pennies(cls, total_cents):\n        dollars = total_cents \/\/ 100\n        cents = total_cents % 100\n        return cls(dollars, cents)\n\n    @classmethod\n    def from_string(cls, amount):\n        match = re.search(\n            r'^\\$(?P&lt;dollars>\\d+)\\.(?P&lt;cents>\\d\\d)$', amount)\n        if match is None:\n            raise ValueError(f\"Invalid amount: {amount}\")\n        dollars = int(match.group('dollars'))\n        cents = int(match.group('cents'))\n        return cls(dollars, cents)\n\n\n# \u041c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441, \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u044f \u0432 \u043d\u0435\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u0432 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e\nmoney_from_ints = Money(234, 45)\nprint(money_from_ints.dollars)\nprint(money_from_ints.cents)\nprint()\n\n# \u0410 \u043c\u043e\u0436\u043d\u043e \u0447\u0435\u0440\u0435\u0437 \u0432\u044b\u0437\u043e\u0432 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043a\u043b\u0430\u0441\u0441\u0430\n# \u041c\u0435\u0442\u043e\u0434\u044b \u043a\u043b\u0430\u0441\u0441\u0430 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0435, \u0430 \u043d\u0435 \u043d\u0430 \u0435\u0433\u043e \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430\u0445\nmoney_from_string = Money.from_string('$140.85')\nprint(money_from_string.dollars)\nprint(money_from_string.cents)\nprint()\n\nmoney_from_pennies = Money.from_pennies(1049)\nprint(money_from_pennies.dollars)\nprint(money_from_pennies.cents)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[112],"class_list":["post-3064","post","type-post","status-publish","format-standard","hentry","category-python","tag-112"],"views":106,"_links":{"self":[{"href":"https:\/\/csnotes.ru\/index.php?rest_route=\/wp\/v2\/posts\/3064","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/csnotes.ru\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/csnotes.ru\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/csnotes.ru\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/csnotes.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3064"}],"version-history":[{"count":1,"href":"https:\/\/csnotes.ru\/index.php?rest_route=\/wp\/v2\/posts\/3064\/revisions"}],"predecessor-version":[{"id":3066,"href":"https:\/\/csnotes.ru\/index.php?rest_route=\/wp\/v2\/posts\/3064\/revisions\/3066"}],"wp:attachment":[{"href":"https:\/\/csnotes.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3064"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/csnotes.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3064"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/csnotes.ru\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3064"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}