November 04, 2021

Modular programming with python – a tutorial for creating a game

Writing with python a small is not very complicated. The existing pygame library allows even newbies in doing so. Most tutorials are assuming that the game is created with the object oriented paradigm That means there are classes for the GUI, for the physics and for the main program. This assumption makes sense because since the advent of the C++ and C# language nearly all games are created this way.
A seldom mentioned technique for creating larger software programs was invented before tor the advent of C++. What was used before is called modular programming and can be realized with python as well. The interesting situation is that modular programs allows similar to OOP to divide a larger project into chunks which are created individual. First thing to do is to create the physics module which contains of a single file.

The formatting has similarity to a class but the class statement is missing. The next step is to create the main module which has also no classes but a variable for drawing the window and two methods. The interesting situation is that the physics module is not initiated as an object but it was only importad and then the main module is sending messages to it.


The game itself consists of a small circle on the screen which can be moved with cursor arrows left and right.


November 03, 2021

Object oriented programming without objects

 

The python programming language provides a seldom explained feature which is the ability to use modules. A module is different from the class concept and has much in common with header files in the c language. The idea is to avoid classes in pyton at all and destribute the coder over many files which are included with the import statement.
From a python perspective the advantage is low.. But a python script which is using only modular programming is much easier to convert into normal c code. That means the written program or game can be made faster in the future by translating the python code into c code. The interesting situation is, that using modules instead of classes is working surprisingly well. What the programmer can is to create small files with less than 100 lines of code. Each file consists of some functions and some variables on top. The variables are only getting accessed from the python module but not from the outside.
What modules doesn't allow is inheritance. Also it is not possible to put different modules into the same file. Each module is stored in a different file. This will increase the amount of files drastically. But thanks the ability to import modules hierarchically it is possible to create very complex applications. With dozends of modules which are combined into submodules.
The chance is high that python programmers are using a same technique. The c language has the disadvantage that it is more complicated to create such modules. Because in addition a header file is needed which is an interface to the outside. But in theory, this concept can replace object oriented classes. That means, there is no need to convert c code into c++ classes.
Modular programming has felt out of fashion since the advent of C++. Today the situation is that more complicated programs are mostly realized with the OOP paradigm. Also the UML notation knows only of classes but not of modules. But in theory both ideas allowing to create larger projects with thousands lines of code.
The only thing what is not working is to avoid classes and modules as well. If somebody writes down 20 variables and 40 functions into the same file it is hard to determine which functions gets access to which variable. Such code can't be maintained. So it is important to divide the code into smaller chunks with less than 100 lines of code. Such a file can be analyzed easily by a human programmer.

Upgrade to Debian 11 makes trouble

 

On some blogs in the internet it was suggested that the upgrade from Debian 10 to 11 is very easy. No it isn't. First problem is to fix the /etc/apt/sources.list file. There are different tutorials how to do so. Theuser has to change the name of distribution, but also the URL to the security mirror server. But suppose the user has carefully recreated the file and has run the “apt upgrade” command. What he can expect then is that only the kernel was updated. After rebooting the machine important programs like the e-mail software evolution or the matplot library doesn't work anymore. But at least the gnome environment seems to be stable. So the user will run for sure the apt full-upgrade command but this will make things worse. After the next reboot the icons on the desktop are missing and it is not even possible to start the terminal program. That means a simple attempt to upgrade the operating system has caused a system wide failure.
Only after manual login into a text only session next to the gnome X11 session and updating the system again will solve the issue. After many reboots, autoremove commands and fixing lots of minor problems it is some sort of miracle that the gnome session is running normal. That means the new Debian 11 system is booting and at least major programs like the Terminal and the Firefox browser can be started.
What we can say for sure is that the upgrade to debian 11 is working with lots of trouble and it is compared to Windows 10 not recommneded for beginner computer users. Linux (especially Debian) remains a construction site and the chance is high that after rebooting the machine the user can't login anymore.

Understanding the LaTeX typesetting system for realizing justified text

 

There are many tutorials available how to create papers and even dissertation projects with the help of LaTeX. What these manuals have in common is that they don't explain in detail why LaTeX is the better choice over word. It most cases the argument is the rendering quality of LaTeX is much higher because of better internal algorithm. To understand what does it mean in detail we have read existing dissertation documents and analyze how the documents are formatted.
What dissertation documents have in common no matter which software was used to create them is, that all of them are formatted with the justified layout. This formatting style is so obvious and so frequently used and has such a long tradition that it isn't mentioned explicit. The typical dissertation is formatted in a symmetric way. That means, the title headline is of couse formatted as center text, and the main body text is also formatted as center text. But for the main body the left and right edge is forming a straight line this is called by typographers a fully justified text.
It depends on the author how exactly this style was realized. A common option is to use the MS-Word software, disable the hyphenation feature and then format the entire text fully justified. The result is that between the words many empty spaces are visible.
Another option used by word authors is to activate the hyphenation feature first and then the rendered justified text has smaller amount of empty space. And exactly this situation is the reason why LaTeX is recommended as a word replacement. Because the LaTeX word wrapping algorithm is able to reduce the empty spaces further. The same text looks with LaTeX different, because LaTeX is using an optimized word wrap algorithm, and very important the microtype package. So the result has much in common with the output of the indesign software which is also able to create high quality text.
So what LaTeX is doing is simple: it creates fully justified text with the help of hyphenation and intelligent word wrapping so that the amount of white spaces is minimized. This ability is labeled by the LaTeX community has high quality output.
So let us go a step back ward and ask a simple question: why exactly is a dissertation formatted as fully justified text, what is about flush left formatting style? No body knows. Even the question is so extraordinary that it is hard to answer it. Basically spoken the paradigm is, that the only allowed formatting style is symmetric, in a way that the headline is centered and that longer texts are formatted fully justified.
This kind of rule is much bigger than the LaTeX community. The rule is valid for other programs like indesign and MS-word as well. The rule is also valid for dissertations written before the advent of the PC. So it is has to do with typesetting in general.
From a technical point of view the LaTeX software can produce better justified text than MS-Word. THis is not a subjective interpretation but a 1:1 comparison will show it. In contrast the difference between LaTeX and indesign is little, both are able to create optimized justified text. The open question is if fully justification in general makes sense. Is there a need to produce centric / symetric documents?
In the history there are two main exceptions from this rule available. Letter are usually created in flush left and the internet based HTML pages are also formatted in flush left. Everything else especially books, journals and dissertations are typesetted in the justified mode.
Perhaps it makes sense to explain the situation from a more positive perspective. A typical introduction into the LaTeX software starts with a direct comparison with MS-Word. On the left side the document is shown formatted with Word and on the right side the same is formatted with LaTeX. Of course the LaTeX rendered pdf documents looks better because the text density is higher. It has little or no white spaces and the page looks similar to a printed book. Because of this ability of LaTeX to generate high quality output, the software is used frequently for academic purposes.
What is not answered in this comparison is the problem of formatting in general. The untold assumption was that both examples (word and latex) have to format the paragraph in the fully justified mode. In this restricted domain, LaTeX is much better. IF the paragraph setting was changed to flush left the result is the same.

November 02, 2021

The case of raggedright in the TeX community

 

It might be surprising but about the most important issue in typesetting the amount of information is little. Most books about LaTeX are explaining what typography is, and why TeX is great but they do not question the difference between left-justification and fully justified text. One of the few exceptions were made in the Tex journal Tugboat [1].
Such a debate is more general than the common question how to realize a certain layout with Latex because fully justified text and typography are strongly connected:
quote “For centuries, book printing has applied justification to nearly all paragraphs” [2]
And yes the statement is correct, a short look into the history of typography will show exactly this preference. Not only documents generated with LaTeX are looking mostly the same, but all the academic books and journals since 300 years have the same standard layout. It contains of two columns which are typeset with justified text. This produces a perfect straight edge and is described by typographers as good typography. That means, the assumption is everything which is not fully justified is equal to low quality typography.
It is impossible to find pro or cons arguments here but what is possible instead is to describe the situation as it is. It is very hard to find books and journals which are typeset with flush left layout. For example, recent issues of the PLOS One journal is doing so. Plos one is a non-traditional digital only academic journal. And perhaps this gives a hint what the idea is. Fully justified text is a sign for printed traditional documents while flush left layout is typical for online only publication.
References
[1] Are justification and hyphenation good or bad for the reader? TUGboat, Volume 37 (2016), No. 2 First results, https://tug.org/TUGboat/tb37-2/tb116akhmadeeva.pdf
[2] Udo Wermuth: An attempt at ragged-right typesetting, TUGboat, Volume 41 (2020), No. 1, https://tug.org/TUGboat/tb41-1/tb127wermuth-ragged.pdf

What Python can learn from C

 

The Python language has become famous because of its object oriented features. This was the main improvement over previous scripting languages like Perl. On the other hand Python has a seldom described features which can be used as a replacement to classes. The concept of a module is to put all the functions in a single file and include this file somewhere else.
Somebody may argue, that a class should be stored in a file and including a module is equal to include a class. The situation is more complicated. Because modules can be used without classes. This programming style is what c programmers are doing: they are creating modules in which the scope of variables and functions is limited and then the module gets included into the main program.
Apart from modules Python support also packages. A package is a directory which contains of many modules. This allows to create larger programs and very important they can be converted easily into c code. That means the original Python program doesn't use classes there for the similarity with plain C is bigger.
The real problem in programming is not to learn a certain language but the problem has how to write programs which are larger than 100 lines of code. The amount of 100 LoC fits well into a single file. It is equal to define a small number of variables and apply 4-5 functions to them. The problem is that real programs will need much more code lines and hundreds of functions. Organizing them into a single file is technically possible but it will become unlikely that the programmer is able to maintain such a file. Object oriented programming provides one possible answer to this problem, but modular programming has also an answer.
Let us describe how to write a module in Python without using classes. The idea is to create a new file and import this file in the main program. The new file contains of variables similar to class variables and methods which can modify these variables. Some of the functions can be accessed from the outside so that the module communicates with the main program. In contrast to the C language the workflow was simplified drastically, there is no need to create dedicated header files and the program will run without a compilation step.

Comparison between flush left and justified text

 

The interesting situation is that there is no real debate which of the formatting styles is better. Other problems in typography can be discussed but everybody knows that it is offtopic to question the idea of justified text. The reason is that the problem of arranging words on a line is seen as equal to typography in general. That means typography is about justified text.
Nearly all the professional books and journals in the last 400 years were printed with justified text. This sounds incredible because in the past no software was used but mechanical typesetting machines. But it seems that the machine experts in the past have solved the problem of word justification over a line. The standard mode of the monotype typesetting machine was that it has procuded justified text and nothing else. The only question which was asked is how to realize this formatting style.
From a technical perspective it is possible to create left justified text with the monotype typesetting machine. And technically, the LaTeX software can do the same for an academic paper. But in the reality nobody is doing so.
To understand the situation we have to go back 100 years and take a look what a human operator at the monotype machine was doing. He gets a manuscript which was either written by a typewriting machine or by hand. And the objective is to convert this manuscript into a justified text stored on a punch tape. The idea behind the monotype machine is to justify the text. This is realized by counting the words, and measuring the spaces, and then the amount of free space is determined. This procedure is repeated for every line.
The interesting fact is the formatting style “Justification” is only one possible formatting style. The text can be read also very well if the step is missing. So why is this step done? Mostly because of tradition. THe idea is that the text is so important and will reach so many readers that the typesetting has to be made with the highest possible quality. And this is equal to fully justified formatting.
This self understanding sounds similar to how books are created before the advent of printing machines. The idea was, that the content of a book is so important that it has to be copied by hand. Creating a copy by hand takes longer and this shows the reverence for the content. Bascially spoken, creating a copy of book by hand is more costly than using a printing press. And creating fully justified text is more expensive than creating only left-justified paragraphs.
Left justified text means basically to lower the quality of typography. Let us make simple calculation. What will happen if the monotype machine is used to create left justified text? Right the duration for typesetting the document is shorter. The human operator needs to calculate less and he can typeset more manuscripts in the same time.

November 01, 2021

Justified text in comparison


To investigate the mystery around justified text, the LaTeX software to render three possible formatting styles: flush left, flush left with hyphenation and fully justified text. The third picture was created with the microtype package to minimize the white spaces in a line. The open question is which of the examples looks better?
What we can say for sure is that fully justified paragraph stands in tradition with typography in general. Most books and academic papers from the past were typeset in this formatting style. While using a flush left formatting is a bit unusual. The interesting situation is that all three examples need 11 lines to show the content. That means even activated hyphenation won't safe space that much.
A second fact is that thanks to the LaTeX internal algorithm the example with the justified text looks very good. That means, the white spaces between the words are not visible and the text looks well formatted. Nevertheless the difference between flush left text and justified text is small. It is mostly a question of personal preference. And most texts in the internet and on smartphone displays are formatted in the flush left mode except pdf documents which are mostly formatted in justified style.
The perhaps most intersesting question is if flush left paragraph formatting is something which has to be fixed. Or let me ask it different. Is there a need for firefox and other browsers to display text justified or is the existing flush left rendering future ready? The situation for screen layout is the opposite as for printed layout. Screen formatting was from the beginning working with the flush left mode only. It is ver
In theory the CSS Specification has the ability to render a text in justified mode, but nobody is using this preference.