[Python-de] Neuer Site-Generator, Erste Schritte
[email protected] (Stefan Ram) 28 Apr 2024 10:29:28 GMT
| Newsgroups | gmane.comp.python.general.german |
|---|---|
| Organization | Stefan Ram |
| Message-ID | <[email protected]> |
Es soll ein HTML-Generator geschrieben werden. Die grundsätzliche
Idee dafür hatte ich unter derselben Betreffzeile schon in
"de.comm.infosystems.www.authoring.misc" beschrieben.
Hier nun die ersten beiden Schritte!
1. Schritt: Die erste Funktion
Ich gehe davon aus, daß die Entität zum Schreiben von HTML eine
Klasse sein sollte, und schreibe zunächst erst einmal nur die
Funktion zur Serialisierung des Textes eines Attributs.
HymarlOfPbml.py
import html
class HymarlWriter:
def attribute( self, text ):
return html.escape( text )
Damit habe ich schon ein lauffähiges Python-Skript, das hier ohne
Fehlermeldung ausgeführt werden kann!
2. Schritt: Der Test
Im Nachhinein betrachtet, hätte ich den Test zuerst schreiben
sollen. Nun ist es aber zu spät! Ich bitte einen AI Chatbot,
einen Test für das obige Skript zu schreiben, und kopiere
diesen in mein Skript hinein. Dies ist nötig, da ich selber
nie gelernt habe, wie man Unit-Tests in Python schreibt!
HymarlOfPbml.py
import unittest
import html
class HymarlWriter:
def attribute( self, text ):
return html.escape( text )
class TestHymarlWriter(unittest.TestCase):
def setUp(self):
self.writer = HymarlWriter()
# Checks that the `attribute()` function correctly handles a normal
# string without any special characters.
def test_attribute_with_normal_text( self ):
text = "This is a normal string."
expected = "This is a normal string."
self.assertEqual( self.writer.attribute( text ), expected )
# Checks that the `attribute()` function correctly escapes special
# characters like `&`, `<`, and `>`.
def test_attribute_with_special_characters( self ):
text = "This & that < more > text."
expected = "This & that < more > text."
self.assertEqual( self.writer.attribute( text ), expected )
# Checks that the `attribute()` function correctly handles an empty
# string.
def test_attribute_with_empty_string( self ):
text = ""
expected = ""
self.assertEqual( self.writer.attribute( text ), expected )
if __name__ == '__main__':
unittest.main()
sys.stderr
...
----------------------------------------------------------------------
Ran 3 tests
OK
Zusammenfassung: Damit habe ich nun eine Klasse geschrieben, die
es erlaubt, einen Attributwert nach HTML/XML zu serialisieren.
Hier, was der Chatbot zu meinem Skript meint:
|Overall, this script is a simple and straightforward
|implementation of a common task in web development, and it
|could be a useful utility in a larger Python project that
|involves generating HTML output.
_______________________________________________
python-de Mailingliste -- [email protected]
Zur Abmeldung von dieser Mailingliste senden Sie eine Nachricht an [email protected]
https://mail.python.org/mailman3/lists/python-de.python.org/
Mitgliedsadresse: [email protected]