Bläddra i källkod

add Pythonic session 43 code

hanojiang 1 år sedan
förälder
incheckning
f8187a09b5

+ 3 - 0
Pythonic/.idea/.gitignore

@@ -0,0 +1,3 @@
+# Default ignored files
+/shelf/
+/workspace.xml

+ 8 - 0
Pythonic/.idea/Pythonic.iml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="PYTHON_MODULE" version="4">
+  <component name="NewModuleRootManager">
+    <content url="file://$MODULE_DIR$" />
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>

+ 6 - 0
Pythonic/.idea/inspectionProfiles/profiles_settings.xml

@@ -0,0 +1,6 @@
+<component name="InspectionProjectProfileManager">
+  <settings>
+    <option name="USE_PROJECT_PROFILE" value="false" />
+    <version value="1.0" />
+  </settings>
+</component>

+ 4 - 0
Pythonic/.idea/misc.xml

@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11 (pyqt_venv) (2)" project-jdk-type="Python SDK" />
+</project>

+ 8 - 0
Pythonic/.idea/modules.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectModuleManager">
+    <modules>
+      <module fileurl="file://$PROJECT_DIR$/.idea/Pythonic.iml" filepath="$PROJECT_DIR$/.idea/Pythonic.iml" />
+    </modules>
+  </component>
+</project>

+ 6 - 0
Pythonic/.idea/vcs.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="$PROJECT_DIR$/.." vcs="Git" />
+  </component>
+</project>

+ 31 - 0
Pythonic/chap-43/ClassDefinition.py

@@ -0,0 +1,31 @@
+
+class TestClass:
+    DEFAULT_TITLE: str = 'DEFAULT TITLE'
+    DEFAULT_STRING_MAX_LENGTH:int = 10
+
+    def __init__(self):
+        pass
+
+    def __call__(self, *args, **kwargs):
+        pass
+
+    def __repr__(self):
+        pass
+
+    @classmethod
+    def some_class_method(cls):
+        pass
+
+    @staticmethod
+    def some_static_method():
+        pass
+
+    # private method
+    def _some_private_method(self):
+        pass
+
+    # instance method
+    def build(self):
+        pass
+
+

+ 73 - 0
Pythonic/chap-43/SideBar.py

@@ -0,0 +1,73 @@
+class SideBar:
+    DIV: str = 'div'
+    H1: str = 'h1'
+    MORE: str = 'more'
+    MORE_ITEMS_LENGTH = 3
+    SHOULD_COMPRESS_HTML: bool = True
+
+    def __init__(self,
+                 title: str,
+                 memu_items: [str],
+                 more: str = MORE,
+                 more_items_length: int = MORE_ITEMS_LENGTH,
+                 should_compress_html: bool = SHOULD_COMPRESS_HTML
+                 ) -> None:
+        self.title = title
+        self.more = more
+        self.should_compress_html = should_compress_html
+        self.memu_items = memu_items
+        self.more_items_length = more_items_length
+
+    def __len__(self):
+        return len(self.memu_items)
+
+    def __repr__(self):
+        return f'SideBar: {len(self)} memu items'
+
+    @classmethod
+    def _header(cls, title: str) -> str:
+        return cls._build_header(cls.H1, title)
+
+    @classmethod
+    def _body(cls, menu_items:[str], should_compress_html: bool) -> str:
+        join_char = cls._get_split_char(should_compress_html)
+        return join_char.join(
+            list(cls._build_body(cls.DIV, menu_items))
+        )
+
+    @classmethod
+    def _more(cls, more):
+        return cls._build_more(cls.DIV, more)
+
+    @staticmethod
+    def _build_header(tag_name: str, title: str) -> str:
+        return f'<{tag_name}>{title}</{tag_name}>'
+
+    @staticmethod
+    def _build_body(tag_name: str, menu_items: [str]) -> str:
+        for menu_item in menu_items:
+            yield f'<{tag_name}>{menu_item}</{tag_name}>'
+
+    @staticmethod
+    def _build_more(tag_name: str, text: str) -> str:
+        return f'<{tag_name}>{text}</{tag_name}>'
+
+    @staticmethod
+    def _get_split_char(should_compress_html: bool) -> str:
+        return '' if should_compress_html else '\n'
+
+    def _is_few_items(self):
+        return len(self) < self.more_items_length
+
+    def build(self) -> str:
+        header = self._header(self.title)
+        body = self._body(self.memu_items,self.should_compress_html)
+        footer = self._more(self.more) if self._is_few_items() else ''
+        split_char = self._get_split_char(self.should_compress_html)
+        html = split_char.join([header, body, footer])
+
+        return html
+
+if __name__ == '__main__':
+    side_bar = SideBar('DEMO SIDE BAR', ['item1', 'item2', 'item3', 'item4', 'item5'], should_compress_html=False, more_items_length=10)
+    print(side_bar.build())

BIN
Pythonic/chap-43/web_example.png