{"id":5247,"date":"2022-06-17T22:04:00","date_gmt":"2022-06-17T20:04:00","guid":{"rendered":"https:\/\/arduino.net.pl\/?p=5247"},"modified":"2023-11-20T21:43:42","modified_gmt":"2023-11-20T20:43:42","slug":"klasy-w-python-ie-slownik-z-nieokreslona-liczba-par","status":"publish","type":"post","link":"https:\/\/arduino.net.pl\/index.php\/klasy-w-python-ie-slownik-z-nieokreslona-liczba-par\/","title":{"rendered":"Klasy w Python-ie. S\u0142ownik z nieokre\u015blon\u0105 liczb\u0105 par"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full is-resized\"><a href=\"https:\/\/arduino.net.pl\/wp-content\/uploads\/2022\/06\/slownik_z_nieokreslona_liczba_par_Bing.jpeg\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/arduino.net.pl\/wp-content\/uploads\/2022\/06\/slownik_z_nieokreslona_liczba_par_Bing.jpeg\" alt=\"\" class=\"wp-image-5919\" style=\"width:840px\" srcset=\"https:\/\/arduino.net.pl\/wp-content\/uploads\/2022\/06\/slownik_z_nieokreslona_liczba_par_Bing.jpeg 1024w, https:\/\/arduino.net.pl\/wp-content\/uploads\/2022\/06\/slownik_z_nieokreslona_liczba_par_Bing-300x300.jpeg 300w, https:\/\/arduino.net.pl\/wp-content\/uploads\/2022\/06\/slownik_z_nieokreslona_liczba_par_Bing-150x150.jpeg 150w, https:\/\/arduino.net.pl\/wp-content\/uploads\/2022\/06\/slownik_z_nieokreslona_liczba_par_Bing-768x768.jpeg 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">&#8222;S\u0142ownik z nieokre\u015blon\u0105 liczb\u0105 par&#8221;. Grafika-AI wygenerowana przez Microsoft Bing.<\/figcaption><\/figure>\n\n\n\n<p>Wpis powsta\u0142 z pomoc\u0105: <strong>ChatGPT<\/strong> <br>Prompt: <em>Przer\u00f3b kod ([wpisa\u0142em ten, kt\u00f3ry <a href=\"#kod1\" title=\"\">pokazuj\u0119 poni\u017cej<\/a>]) na klas\u0119, kt\u00f3ra obs\u0142ugiwa\u0107 b\u0119dzie urz\u0105dzenia IoT o r\u00f3\u017cnych nazwach i r\u00f3\u017cnej ilo\u015bci parametr\u00f3w do przesy\u0142ania i obs\u0142ugi.<\/em><br><strong>ChatGPT<\/strong>: Oto klasa <code>IoTDevice<\/code>, kt\u00f3ra obs\u0142uguje urz\u0105dzenia IoT o r\u00f3\u017cnych nazwach i r\u00f3\u017cnej ilo\u015bci parametr\u00f3w do przesy\u0142ania i obs\u0142ugi:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; light: false; title: Kod:; toolbar: true; notranslate\" title=\"Kod:\">\nclass IoTDevice:\n    def __init__(self, device_name, **device_params):\n        self.device_name = device_name\n        self.params = device_params\n\n    def send_data(self):\n        # Symulacja przesy\u0142ania danych do chmury lub innego systemu\n        return f&quot;Data from {self.device_name} sent: {self.params}&quot;\n\n    def receive_data(self, new_data):\n        # Symulacja odbierania danych przez urz\u0105dzenie\n        print(f&quot;{self.device_name} received new data: {new_data}&quot;)\n\n# Przyk\u0142ady u\u017cycia klasy\nthermometer = IoTDevice(device_name=&#039;Thermometer&#039;, temperature=25.5, humidity=60)\nprint(thermometer.send_data())\n\nsmart_lock = IoTDevice(device_name=&#039;SmartLock&#039;, status=&#039;locked&#039;)\nprint(smart_lock.send_data())\n\n# Symulacja odbierania danych przez urz\u0105dzenie\nnew_data = {&#039;temperature&#039;: 26.0, &#039;humidity&#039;: 62}\nthermometer.receive_data(new_data)\n<\/pre><\/div>\n\n\n<p>W tym przypadku klasa <code>IoTDevice<\/code> przyjmuje nazw\u0119 urz\u0105dzenia (<code>device_name<\/code>) oraz dowoln\u0105 liczb\u0119 parametr\u00f3w (<code>**device_params<\/code>), kt\u00f3re s\u0105 traktowane jako s\u0142ownik. Metoda <code>send_data<\/code> symuluje przesy\u0142anie danych z urz\u0105dzenia do chmury lub innego systemu, zwracaj\u0105c odpowiedni\u0105 wiadomo\u015b\u0107. Metoda <code>receive_data<\/code> symuluje odbieranie danych przez urz\u0105dzenie.<\/p>\n\n\n\n<p>W przyk\u0142adowych u\u017cyciach utworzono dwa obiekty klasy <code>IoTDevice<\/code> reprezentuj\u0105ce termometr i inteligentny zamek, z r\u00f3\u017cnymi zestawami parametr\u00f3w. Nast\u0119pnie wywo\u0142ano metod\u0119 <code>send_data<\/code> dla ka\u017cdego z urz\u0105dze\u0144, a tak\u017ce przeprowadzono symulacj\u0119 odbierania danych przez termometr za pomoc\u0105 metody <code>receive_data<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Na podstawie: W3D1: <a data-type=\"URL\" data-id=\"https:\/\/youtu.be\/03yGTxh2TWo\" href=\"https:\/\/youtu.be\/03yGTxh2TWo\">Instantiating Class Objects with Hashes, Arg &amp; Kwargs (3 of 4)<\/a><\/p>\n\n\n\n<p>Tworzymy szkic ClassEmployee.py:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Employee:\n    def __init__(self, **employee_info):\n        for item in employee_info:\n            print(item)\nnew_employee = Employee(first_name='Jan', last_name='Kowalskil')<\/code><\/pre>\n\n\n\n<p>Wynik w terminalu po uruchomieniu programu:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>first_name\nlast_name<\/code><\/pre>\n\n\n\n<p>Dodajemy linijk\u0119:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(dir(new_employee))<\/code><\/pre>\n\n\n\n<p>Zostaj\u0105 wy\u015bwietlone metody dost\u0119pne w klasie:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;'__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']<\/code><\/pre>\n\n\n\n<p>U\u017cyjemy metody <strong>__dict__<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Employee:\n    def __init__(self, **employee_info):\n        self.__dict__.update(**employee_info)\n\nnew_employee = Employee(first_name='Jan', last_name='Kowalskil')\nprint(dir(new_employee))<\/code><\/pre>\n\n\n\n<p>Wynik w terminalu po uruchomieniu programu:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;'__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'first_name', 'last_name']<\/code><\/pre>\n\n\n\n<p id=\"kod1\">[kod1]<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Employee:\n    def __init__(self, **employee_info):\n        self.__dict__.update(**employee_info)\n    def say_hello(self):\n        return f\"Hello, my name is {self.first_name} {self.last_name}\"\nnew_employee = Employee(first_name='Jan', last_name='Kowalski\nprint(new_employee.say_hello())<\/code><\/pre>\n\n\n\n<p>Wynik w terminalu po uruchomieniu programu: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt; Hello, my name is Jan Kowalski<\/code><\/pre>\n\n\n\n<p>Alternatywne tworzenie s\u0142ownika z dowoln\u0105 ilo\u015bci\u0105 element\u00f3w. Ta wersja dzia\u0142a z MicroPython-em na ESP32. Podana wy\u017cej z powodu braku modu\u0142u <strong>__dict__<\/strong> zg\u0142asza b\u0142\u0105d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Employee:\n    def __init__(self, **employee_info):\n        for key, value in employee_info.items():\n            setattr(self, key, value)\n    def say_hello(self):\n        return f\"Hello, my name is {self.first_name} {self.last_name}\"\n\nnew_employee = Employee(first_name='Jan', last_name='Kowalski')\nprint(new_employee.say_hello())<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Wpis powsta\u0142 z pomoc\u0105: ChatGPT Prompt: Przer\u00f3b kod ([wpisa\u0142em ten, kt\u00f3ry pokazuj\u0119 poni\u017cej]) na klas\u0119, kt\u00f3ra obs\u0142ugiwa\u0107 b\u0119dzie urz\u0105dzenia IoT o r\u00f3\u017cnych nazwach i r\u00f3\u017cnej ilo\u015bci parametr\u00f3w do przesy\u0142ania i&#8230;<\/p>\n","protected":false},"author":3,"featured_media":5919,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[141,65,59],"tags":[174,175,173,150,155],"class_list":["post-5247","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-micropython","category-programowanie","category-python","tag-class","tag-iot","tag-klasy","tag-kwargs","tag-python"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/posts\/5247","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/comments?post=5247"}],"version-history":[{"count":21,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/posts\/5247\/revisions"}],"predecessor-version":[{"id":5932,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/posts\/5247\/revisions\/5932"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/media\/5919"}],"wp:attachment":[{"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/media?parent=5247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/categories?post=5247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/tags?post=5247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}