1. Trang chủ
  2. » Công Nghệ Thông Tin

Technical 5 box set chromecast, linux, XML, PHP, python

496 46 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

XML PROGRAMMING THE ULTIMATE GUIDE TO FAST, EASY, AND EFFICIENT LEARNING OF XML PROGRAMMING (XML SYNTAX, XML PROGRAMMING, DTD’S, XML PARSERS, HTML5) Table of Content Introduction Chapter 1 - What’s in a Markup language? Chapter 2 - Why XML when there is HTML? Chapter 3 - Show me the Syntax and Semantics Chapter 4 - Document Type Definition Chapter 5 - Get your hands dirty Chapter 6 - Interpreting XML - XML Parsers Chapter 7 - HTML5, why here? Conclusion Introduction Have you ever felt learning XML as a nightmare? Discouraged by the huge volumes of beginner guides? I understand the difficulty of extracting the core from a book which runs to few hundred pages It is not very interesting to read few thousand lines for creating a simple XML document on your own How would you feel, if I say I can do that job for you? I have read through hundreds of pages for you and will give you the ready to drink juice from the fruit This book is a quick and easy guide, an extract from those heavy pillow-sized books, menacingly long examples and boring high level jargons I wrote this book keeping in mind all the difficulties I faced as a beginner and thus the extract is simple, understandable and short With this brief introduction, I request your attention and welcome you to travel through the book with me Chapter 1 - What’s in a Markup language? Like all human languages, computer languages are also developed for the purpose of communication Human to computer communication, computer program to computer program communication are supported by various computer languages Every language that is developed has a set of rules and procedures and we call them syntax Languages can be grouped based on the type of communication they are developed for Markup languages were primarily designed to instruct the software that displays text; they are designed to deliver information over the World Wide Web Markup is a meta data, when it is added to a text or document it enhances its meaning Precisely, markup language is a set of tags when added to a document can demarcate and highlight the various parts of that document Markup languages use tags which contain the necessary specifications for processing, defining and presenting a text One of the most popular markup languages is Hyper Text Markup Language, they have predefined semantics for presentation Their greater use is in describing the structure of a website semantically along with cues for presentation The predefined elements in a HTML include: headings, paragraphs, lists, tables, images, and hyperlinks This is how a simple HTML code looks like: SNIPPET 1 Markup Language HTML This is a sample HTML page

SGML stands for Standard Generalized Markup Language SGML is not in itself a document language

  • Markup
  • HTML Page
  • XML Sample
tidbits Plain text: John Mathews HTML File (Displaying the plain text, using pre-defined tags): Name

John Mathews

Did you find something inside the markup vessel? Chapter 2 - Why XML when there is HTML? XML is an acronym for eXtensible Markup Language; they provide means to share data and information between computer programs or computers While HTML enables its users to process, define and present text using predefined presentation semantics, XML is a meta-markup language Unlike HTML or troff in XML there aren’t any predefined tags, you need to create your tags and semantics as and how your communication demands Also, XML concentrates on describing the structure and meaning of a document, it does not show how a document will look like It does not describe the document’s format but style sheets can be used for formatting an XML document Once the data is shared, it is the receiving computer program’s discretion to interpret useful information from them Initially, XML was created as a means of web publishing but later, their usage unfolded in new arenas XML is human readable, language independent; it can be shared across platforms and OS independent XML is not only used to render HTMLs, it also renders raw data, which for instance can be processed and fed into databases Now, there would arise an obvious question, why XML, a simple text file can serve the purpose Yes, a text file can very well be shared across platforms and can carry raw data but, the structure of data shared cannot be defined beyond a table or matrix in a text file Whereas, XML can define relationship between entities, they can define hierarchical data structure Chapter 11 - Creating a Module Sometimes you may want to extend the functionality of a Python program yourself by creating your own modules Here is how you go about it Open shell and create a new Python script and save it as sub_mod.py somewhere on your desktop Place the following code into the file and save it: def subtraction (num1, num2): answer = num1 - num2 return (answer) Congratulations, you have created a module It has a function named subtraction that takes in two numbers num1 and num2 as parameters The line answer = num1- num2 subtracts them and assigns them to the answer variable Finally, we use this line return (answer) to return a value Now you can use this module in any program however you like Just import it Now create another file and call it sub_imp.py and save it on your desktop as well Place the code below in that file: import sub_mod answer = sub_mod.subtraction(8, 3) print(answer) As you can see, all we had to do is import the module we created using the line import sub_mod We then call subtraction() functionfrom the sub_mod module, passing in two values then assigning the returned value to a variable with this line answer = sub_mod.subtraction(8, 3) Then we use the variable to output the answer with the line print(answer) The output of subtracting 3 from 8 is 5 and that is what will be printed out For this to work, both files need to be in the same directory You can place them in different directories then append the file path where the file is located to the list of directories that python goes through to search for files and modules But we won’t be discussing that in this book to keep things simple In the final chapter of this book, we shall look at how we can work with text files Python grants you the ability to read and write and also rename and delete files with ease This goes beyond asking input from user only to actually processing data from external files and even storing data in them All this is in the next chapter Keep reading Chapter 12 - Working With Files in Python Here is what we will cover by the end of this chapter: Opening and Reading a Text File Writing to a Text File Renaming and Deleting Text file So far we have been able to get input from the user using the input()function We did this in Chapter 6 As you recall, this prompted the user to enter something But that is not all Python can do Python can also retrieve data from external files such as text, binary and image files then print that information to the user We are going to be working with text files in this chapter We will be opening, closing, reading, writing, renaming and deleting text files There are a lot of things to do in this chapter Shall we get started? Open and Reading a Text File Before we open a text file, it needs to exist Open up any text editor enter the following three lines and save in the same directory as were the program will be saved Name the file example.txt: Lights will guide you home And ignite your bones And I will try to fix you Okay, you got me They are lyrics from another Coldplay song called “Fix You” Did I mention how I really love Coldplay? The syntax to open a file in Python looks something like this: open(‘name of the file’, ‘file mode’) The first parameter is where you enter the path to where the file is located Since our file is located in the same directory as the program, the path will simply be‘example.txt’ The second parameter is for the file mode we shall be opening the file in Here is a list of some of the file modes we can use: ‘r’mode – This means the file has been opened in read-only mode ‘w’mode – This means the file has been opened in write-mode for only writing to it Also, if the file does not exist yet, it will be created But if the file exists, all existing data will be erased ‘a’mode – This opens the file in append-mode for appending data at the end of the file If the file does not exist, it will be created and if it exists, the data will not be erased ‘r+’mode – This will open the file in both read and write mode Now that we know a bit about theopen()function, to open and read form a file in Python is easy Create a new file and place the following code in it: #opening a the text file example.txt example_text_file = open(‘example.txt’, ‘r’) #read some lines and print them to the screen first_line = example_text_file.readline() second_line = example_text_file.readline() print(first_line) print(second_line) example_text_file.close() To begin with, the line example_text_file = open(‘example.txt’, ‘r’) will open a text fill and set it to read-only mode then assign the resulting text object in to the variable example_text_file To read from a text file, you use the readline() function which always reads the next line from a file So the first example_text_file.readline()line will read the first line from the text file then assign the resulting string value to variable first_line and the second one will read the second line and assign it to the variable second_line Reading from a text file is as easy as that The next lines will just print out the values of the first_line and second_line variables to the screen, We then use the close() function to close the file since we are done using it This will free up space in memory for our program The line example_text_file.close() does this for us in our program If you save and run the program, you should get the following output: Lights will guide you home And ignite your bones Okay, now let us look at writing to the same text file Writing to a Text File The code for writing and appending to a text file is basically the same All you change is the file mode Since write-mode will erase all the data, I will use append-mode instead and just add a line of text to the end of the text file that we created in the previous section Just note that opening it in this mode means that we can no longer read from it Create a new file and save it the same folder as the example.txt then place the following code in there for appending: #opening example.txt in append mode to avoid erasing the data example_text_file = open(‘example.txt’, ‘a’) #this appends a line of text at the end of example.txt example_text_file.write(‘\nI forgot how the rest of it goes’) example_text_file.write(‘\nI need to look up the lyrics’) example_text_file.close() Since you already know what the open()function does, I bet you have a pretty good idea what the line example_text_file = open(‘example.txt’, ‘a’) does Yes It opens the text file example.txt in append mode We then use the write() function to write to our file and since the file is in append mode, the lines example_text_file.write(‘\nI forgot how the rest of it goes’) and example_text_file.write(‘\nI need to look up the lyrics’) will be added to the end of the file We use the \n (new line) escape character to write each of them on a new line; otherwise, they will both be added to the last line of the file The last line in our program just closes the file since we are done using it If you save and the run the program, the txt file example.txt should look something like this when you open it: Lights will guide you home And ignite your bones And i will try to fix you I forgot how the rest of it goes I need to look up the lyrics Renaming and Deleting the Text File The functions we need to use for renaming and deleting a file are in the os module So you need to import them into your program before you can use them The syntax for renaming a file in Python is rename(‘old file name’, ‘new file name’) So if you wanted to rename the text file example.txt to something like renamed.txt, you would write rename(‘example.txt’, ‘renamed.txt’) Removing a file is even easier The syntax to remove a file in Python is remove(‘file name’) To remove the example.txt, you would write remove(‘example.txt’) That is enough fun with files In fact, that is enough fun with this book as well You have made it to the end But before you go, let us just quickly recap what we have learned as I conclude the book in the page Conclusion It is my sincere hope that if you here, then this book endowed you with awesome programming abilities that you can improve upon as you head into the awesome world of programming And I also hope that you use your abilities for good because, as you already know, with great power comes great responsibility It is now up to you to create programs that help you or others Programming makes life easier Let us briefly recap what we covered in the chapters of this book In the first chapter, we covered how to install the Python 3 interpreter to be able to run our programs We also looked at Shell and IDLE then used them to create a simple ‘Hello World’ program Finally, we looked at comments and the print function a little In the second chapter, we dove deeper into Python by looking at variables and how they act as containers for data types We created them, assigned values to them and looked at some arithmetic and assignment operators we can use to manipulate them In the third chapter, we looked at the various basic data types that the Python language supports, such as the numeric, string and list data types We looked at various functions for the numeric and list types that you can use to manipulate them and also how to combine and format strings The fourth chapter looked at how we can get input from the user and how to convert it into different data types We also saw a little more of the print function in action by looking at multiline printing and how to use escape characters In the fifth chapter, we were able to control the flow of our program by enabling it to be able to make decisions on which code blocks to execute based on conditional statements But not before we looked at the comparison and logical operators we can use in conditional statements In the sixth chapter, we were able to introduce how to loop through code blocks until a certain condition was met then used that to print out information from lists and create countdowns In the seventh chapter we covered functions We looked at built-in functions then how to create our own functions; call them, pass them information and have them return back information for us to use in our program In the eighth chapter we looked at how to import modules into our programs that give us access to various variables and functions that enable us to extend the functionality of our program We even learned how to create and import our very own models In the ninth and final chapter, we looked at looked getting input from external files such as text files and how to manipulate them Now that you have mastered the basics, you are at a position to tackle the advanced topics and look into object oriented programming and how you can create classes You can also look into advanced collection types like dictionaries or how to retrieve and display images or read binary files You have so much more to explore But don’t despair I know you can do it ... Chapter 3 - Show me the Syntax and Semantics Chapter 4 - Document Type Definition Chapter 5 - Get your hands dirty Chapter 6 - Interpreting XML - XML Parsers Chapter 7 - HTML5, why here? Conclusion Introduction Have you ever felt learning XML as a nightmare? Discouraged by the huge volumes of beginner guides? I understand... Before closing the tag element , the tag elements and must be closed The snippet 5 shows the proper way of nesting SNIPPET 5 Reebok 4... 25< /messages> SNIPPET 7 4 2 25< /messages>

Ngày đăng: 04/03/2019, 16:46

Xem thêm:

Mục lục

    Chapter 1 - What’s in a Markup language?

    Chapter 2 - Why XML when there is HTML?

    Chapter 3 - Show me the Syntax and Semantics

    Chapter 4 - Document Type Definition

    Chapter 5 - Get your hands dirty

    Chapter 6 - Interpreting XML - XML Parsers

    Chapter 7 - HTML5, why here?

    Chapter 5 - User Management

    Chapter 6 - Linux Tips and Tricks

    Chapter 7 - Vi Editor

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN

w