Guest Menu

SeSDoCPy -- Python Committee

Demonstrating Checkboxes &c with Python

Welcome!

SeSDoC's main purpose is to demonstrate handling HTML Check Boxes and other HTML FORM elements, including validation of users' input. SeSDoC doesn't update a database, stops short of that to facilitate debugging and validation of the project at W3C and google's mobile friendly test.

The next exercise requires authenticated users, updates the database with membership referrals, and reports on them...

The instructor has provided sample code for INFO465 projects using VB.NET, C#, PowerBuilder, and PHP for the past several years. Recently, more students have asked for Python examples for 465 projects, relating that Python's in the job descriptions for network management and app development situations, but they didn't find on-line resources to do 465 projects.

SeSPoPPy is new in 2018, hoping it'll help lead to good-looking Python for students' portfolios...

LAMP - #1 Most Deployed Web Server

A 'LAMP Stack' is: Linux OS - Apache web server; MySQL/MariaDB database; PHP, Python, Perl, or Ruby programming language.

The Linux may be deployed on a real or vitrual server running on Intel or AMD x86, IBM's Power or zSeries, ARM or ARM64 by any of several manufacturers, Sun/Oracle's Sparc, and most of the single-board machines like Raspberry Pi.

LAMP has been adapted to WAMP-Windows with AMP, MAMP-Mac OS9, and XAMP-OSX plus AMP. Server applications are easily portable among LAMP, XAMP, MAMP and many of the other 'stacks' that are an easy install. Sometimes all that's needed is to change the tilt of slashes between Windows' \ and unix's /.

SeSPoPPy runs on a LAMP stack on a virtual CentOS server somewhere at Digital Ocean, where the P is for Python. PHP and Perl are other options for the P in LAMP. PHP examples for database programming are at SeSPoP and SeSDoC.

Perl is another option for the P, but has fallen from favor as a web programming language after some earlier years where CGI & Perl were one of few options for handling FORMs as they were added to HTML standards along with GET and POST data way back when...

Ruby is nearby in the alphabet, just before P, and is often used on Rails to top off a stack of Linux, Apache, and MySQL/MariaDB with an R instead of a P. Ruby on Rails provides a powerful and easy to secure MVC-Model View Controller. Used this way, Ruby is somewhat abstract and has a moderately steep learning curve, which is well worth climbing.

Apache is an open source web server released in 1995, has been the most-used web server since 1996. It's included with most Linux distributions. Other open source options are NGINX, Tomcat, or Node.js but Apache reigns at more than 80% of the share for for web servers. IBM's IHS and WebSphere are proprietary options with open source cores used on IBM's i, z, and Power platforms. Microsoft's IIS continues to gain some share where other MS servers are running.

MySQL and, more recently MariaDB, make the M in the LAMP stack. Both are 'semi-open-source', asking for a small license fee for commercial use. MySQL is a blazing fast server that is just growing out of 32-bit, where it was limited to databases of 4GBytes, so it has been easy to point at it and say 'it is not an enterprise-class database'. It was blazing fast because it enforced few of the ACID requirements, couldn't do transactions, and didin't enforce referential integrity. Today's 64-bit versions are proving that they can grow to enterprise scale, but they remain the most-used DBMS for _small_ servers and proprietary DBMS like IMS, DB2, and Oracle manage more data. MySQL recently became the property of Oracle, regarded as The Devil by some of the open-source community, and the original developer 'forked' it back to a more purely open-source development model as MariaDB.

Demo Code

This code demos HTML FORM handling including checkboxes and multi-selects, radio buttons and other html form elements. It stops short of updating a database, which is covered in a later assignment. It's easier to debug, validate, and score mobile-friendliness of a form that doesn't require authentication. The sample code is easy to modify for database update, so your code can be re-used for later deliverables.

HTML FORM elements like checkboxes, radio buttons, and selects present a special challenge where the user expects them to be checked, pushed, or selected the same as they set them on a form every time they see it. Over the decades the instructor has mentored students on projects, meeting this expectation has proven to be the most difficult whether its in a desktop or web context.

SeSDoCPy Setup

About Python vs. PHP

Python and PHP are two of the most used languages for the LAMP stack, so they invite comparisons.

Of the two, Python is the elder, launched in the '80s, not for the web. Python find lots of uses for programming for system administration, stats, science, networking, security, and interfaces with lots of proprietary and open-source applications like LibreOffice or GIMP.

Python runs directly at the command line, or from a script that is interpreted from the source code.

Python implements objects in a very simple scheme where one module may 'import' another module and easily use its data and functions. For most intents the modules act like 'static classes' in other languages that only have one instance that is global in scope. Modules may also define 'dynamic classes' which may have multiple instances as objects.

PHP is elegant for web apps, but is somewhat clumbsy at the command line.

What PHP does transparently through it's super-global data structures or in a single line of code, Python requires a few or several lines of code that reference a library, and a programmer who's aware of the syntax for raw HTTP, files, and objects in the DOM and CGI and can find and import the apprropriate libraries to handle them.

Python is Interpreted or Compiled to Byte Code before Interpretation

Python is a very portable, general purpose, programming language used for all kinds of open-source programming tasks in unix and windows networking and administration. It works in Linux, many unices, Windows, Mac 9 and OS X, iOS, and Android.

Requirements for extreme speed, efficiency, or keeping source-code secret are reasons that many developers might _not_ use Python, PHP, or other interpreted or byte-coded languages like Java or C#.

Python may be compiled and distributed as byte code, preserving secrecy of source code, but not enhancing efficiency much. There are a few options for compilation: The native CPython-Compiled Python for Linuz and unices; IronPython for .NET framework, Jython for JVM-Java Virtual Machine, and the JIT-compiled PyPy. If somebody wants to protect their source code there are ways to do it with Python.

The cost of byte code: For some applications Byte-code can never be as quick as binary executables optimized for the hardware platform's CPU. In these cases, neither Python, PHP, nor Java is a good option.

Binary Executables May be the Quickest!

Binaries compiled from C or C++ or assembled from a CPU's assembly language are distributed where software components need to be the quickest, or their source code is secret. It's possible to 'reverse compile' binary code but the result is seldom anything like 'maintanable code' and can require extremely talent resources to support. The adage about reverse compilation is: You can't turn the grinder backwards and get the pig back out...

Extreme Performance Sometimes Isn't Important! Secret Code Sometimes Is!

Don't dismiss interpreted open-source and byte-coded languages where they might be valuable just because well-coded binary executables can be faster! And, not everybody who wants to protect their source code is evil.

For many business and personal computing applications extreme performance makes little or no difference in the customers' experience. Byte coded or interpreted languages like PHP, Python, C#, VB, ASP or Java work fine.

Byte code also offers a layer of obfuscation to help keep source code secret. It can be coded by ordinary programmers using powerful 3 and 4GL web langagues quicker/cheaper, saving the higher-dollar assembler or C coders for applications' components where speed _is_ important..

Virtual machines for Java, .NET framework, or Python perform well enough on any platform to satisfy almost everybody but gamers for most purposes. Games and other high-performance applications like video-compositors or statistical apps need binary executables, not byte-code. Games, other high-performance apps are usually distributed as binary executables, compiled or assembled for the target platform. If extreme performance isn't a requirement, byte-coded and interpreted languanges make sense and may satisy requirements for secrecy of source code.

Windows since XP includes the .NET framework, which also runs on Mac, Linux, iOS, OSX, or Android. The .NET Framework runs byte code compiled from VisualStudio's ASP, C#, or VB.NET. Visual Studio is an excellent GUI 4GL development environment and is an easy option for any organization supporting Windows desktops and servers. Visual Studio's byte code is somewhat easier to reverse-compile than ginaries, but it may be further obfuscated as it is prepared for distribution to help keep secrets.

If the developer is well-aware of all the functions that the JRE-Java Runtime Environment, .NET Framework, Or Python libraries provide byte-coded applications can be very efficient.

Python works with practically all interfaces the OS provides to file systems, CGI-Common Gateway, networks of most types, hardware abstraction for many laptop and hand-held devices, and databases traditional and recently-emerged. Open Office, LibreOffice, Blender3D, GIMP, Glade, and lots of other open-source applications for telephones, desktops, and networking involve Python. Python's scheme for importing libraries and handling objects makes it easy for developers to adapt it to almost any requirements.

Again, outside of a web server environment, PHP is clumbsy compared to Python. For most web apps, PHP is an elegant thing, but it may not be the best choice for other environments. Python's libraries have extended it to web apps, database programming, and all that a server needs, along with most everything for scientific, stats, process control, and engineering applications. Python provides a highly structured, object-oriented, scalable application environment in Windows, Mac, Linux, or unix.

PHP - Optimized for Web Development

PHP scripts were developed to be embedded in HTML code, to be interpreted by the Hyper Text Preprocessor added to Apache as a module. This makes a very efficient way for a server to merge HTML with active content.

PHP didn't have a CLI-Command Line Interface until recent releases, only had CGI and raw STDIN, STDOUT, and STDERR. PHP was clumbsy without Apache. Those of us who used it for administrative scripting wrote our own CLI using the raw input, output, and error streams.

PHP also didn't introduce an Object model until recently, where Python has been object-oriented since it's inception. In PHP, the adage "Structures scale better than Objects" holds true, in the instructor's humble opinion, and PHP apps may be engineered to scale exceptionally well in mid-range or mainframe platforms...

PHP is often embedded in HTML documents, using <?php to 'escape' into and php until >> is encountered. Or, PHP knows how to make its own HTTP headers and abstract all the elements of the CGI interface into 'super global' variables like $_SESSION, $_REQUEST, or $_COOKIE.

PHP makes it relatively easier for an inexperienced programmer to learn the environment and get up to speed, while the noobish Pythoner is still finding the libraries and inventing the bits of code to manage sessions or cookies.

Either PHP or Python can yield excellent return on investment for a system's owners -- it's hard to beat open-source languages, freely available, for value! C# community, Java Net Beans, and other free IDEs help introduce these proprietary languages to the community. But Visual Studio Pro & Enterprise Net Beans, or Zend Studio come with a hefty price tag -- this makes them less attractive for 'small shops' who don't want to shell out thousands of dollars for languages...

Immutability of Strings, Thrashing in Either PHP or Python

Immutability of strings is a big issue whether optimizing PHP or Python! Using syntax like String = String + SomethingElse or $String .= $SomethingElse is expensive in either language -- especially if String is used in a loop!

Pythons strings are immutable, so whenever a change or concatenation is made to a string the whole string gets copied and the prior string is garbage, wasting RAM. PHP's strings are somewhat less immutable, but googling it suggests that it's always better to avoid involving long strings in loops.

An undisciplined approach to building a string, like an HTML document, can use 10 or 100s more RAM and CPU than is optimal! When students' Python or PHP scripts blow past their RAM limits or they bloom up in the performance monitor before crashing large strings are sometimes involved.

It's usually better to build controls like Checkboxes separately, like CBox = CBox + Option rather than use a construct like UI = UI + Option in a loop!

More Comparisons

Python uses \{\} and the .format method the best, can handle large blocks of HTML between triple-quotes, """, followed by a long tuple of variables to replace each \{\}. This way, the long string only needs to be bandied about once.

PHP is best with multi-lined, double-quoted strings with simple $Variables in them.

PHP automates much of what it takes for database programming for the web, making it relatively easier for a novice programmer to get up to speed with it. Python requires the programmer to be aware of more elements at the web and operating system interfaces and write a bit more code to make them work.

Once written, Python code is easily reused, so a few extra lines of code don't necessariy translate to more time spent coding...

We admire Python for it's very lean syntax where white space is significant and indentation signifies logical structures! PHP's Squiggly brackets, other in-line punctuation like $ for variables, and semi-colons are cluttered in comparison.

Python's object notation is period-delimited and uses a very simple scheme to import objects and access their functions as methods and variables as properties. Python and PHP are both structured and object-oriented lanaguages, with very few other similarities.

PHP uses similar syntax to C and Java with squiggly brackets to indicate structures, ==> object notation, and those pesky semi-colons at the end of each statement. Python has a syntax _not_ derived from C and uses indentation (two spaces is optimum) to indicate structures so it doesn't need squiggly brackets or semi-colons.

PHP variables all start with $, so it's easy to insert them into double-quoted strings. Pythons variables start with letters of the aplphabet, and are inserted into string using \{\} placeholders and the format function.

Native Python & Python Frameworks

There's lots of value in learning to code web apps with 'native python' as demo'd here. Raw Python requires a programmer to invent web constructs like sessions, ouput raw HTTP headers, find the appropriate libraries, and make Checkboxes that behave. Development may be slow and risky as skills and techniques are developed.

There's also lots of value in learning a Python framework or two. I've seen students come up to speed very quickly with Django, Flask, or CherryPy. A talented student who understands how Django works can complete all of the coding for INFO465 projects in a couple of hours while an equally talented student who wants to work with native Python is still figuring out how to make sessions and forms work.

The best frameworks are put together by smart people who understand Python and application development very well.