Linux Containers Authors: Liz McMillan, Elizabeth White, Zakia Bouachraoui, Pat Romanski, Stefana Muller

Related Topics: Linux Containers

Linux Containers: Article

Variety is the spyce of Python

More tricks, tips, and techniques for making spyce work with your server.

(LinuxWorld) — Python is one of the most versatile languages on the planet. Here are a few examples of the language's well-roundedness:

  • You can use it to write sophisticated object oriented works of art
  • You can toss together something akin to a shell script
  • It can be multi-platform
  • It can be platform-neutral
  • It can generate Java byte codes
  • You can write full-blown GUI applications with Python
  • You can choose from among several GUI frameworks including Qt and Gtk
  • Python makes a dandy Web-application language

In my last article, I described a relative newcomer to the Python server-side scripting solution category: spyce. Spyce is a versatile means of integrating Python code into HTML and vice versa.

If you're coming from a PHP background, you may not get the results from spyce and Python that you have come to expect from PHP. Whether this is a good or bad thing is something you'll have to decide for yourself, but you should be aware of these differences before you start a spyce project.

One obvious difference between spyce and PHP is in how each are interpreted or compiled. This affects code that you may want to keep in files to share among projects by including those files. The interpretive nature of PHP tends to allow you to be more dynamic with your methodology than spyce, although you can work around the differences if you get used to the way spyce and Python work.

There are some basics you'll need to know before we get to the differences. First, the simplest way to create a spyce document is to embed Python statements into an HTML page, one at a time. You can do that by enclosing a single statement in double square brackets such as, [[statement]]. For example:

Hello [[print 'World!']]

You can also print the contents of a variable directly, without using the print statement, by adding an equal sign after the opening brackets. For example, if the variable welcome is set to World!, then you can use the following to print Hello World!. Obviously, you'll need to set the value of the variable welcome ahead of time.

Hello [[=welcome]]

If you want to write a series of Python statements, add a backslash to the opening brackets. This method is especially convenient if you want to generate even the HTML from Python. For example:

welcome='Hello World!'
print '<html><body>'
for i in range(5):
  print welcome
print '</body></html>'

Note, of course, that the print welcome statement must be indented for the statement for i in range(5): to affect it. That's the primary way Python parses code. (Flame bait: You can use curly braces, too, but real Python programmers use whitespace.)

Even when we write the code in statements enclosed in brackets separately, the value of welcome is persistent through the main file and can be set or changed by any statically included files. For example, let's create a separate spyce file where we'll put commonly used functions and classes. We'll call this file classes.spy. We'll put two functions and two classes in it. (It's not needed here, but note that triplets of single-quotes are possible in spyce, just as they are in Python.)

File: classes.spy

   def openHTML():
      print '''<html><body>'''

def closeHTML(): print '''</body></html>'''

class doHello: def printHello(self): for i in range(5): print 'Hello world<br>'

class themeColor: def getColor(self): return 'blue' ]]

Now we can include the classes.spy file in our main index.spy file and make use of the classes and functions. You do this with the spyce directive include. Spyce parses a statement as a directive when you add a dot after the opening brackets, such as [[.directive]]. First, the code generates the opening html, then plays with a couple of classes from the classes.spy file. You instantiate the themeColor class with the simple statement c=themeColor(), after which you can access its methods.

File: index.spy

[[.include file=classes.spy]]
c = themeColor()
tc = c.getColor()
print tc
x = doHello()

When it comes to sharing information and included files, however, the operative word is static. The information in classes.spy is available to our program because we used the static method of inclusion, which is the directive .include. This directive only takes a static string. One cannot specify the file name with a variable so that your program decides which file to include as it executes.

How to include files dynamically

Fortunately, Spyce also features a method to dynamically include files. Here's how you might do that. I've also structured the program a little differently so that I can group the various stages of execution in a way that is visually appealing to my style of programming. For example, the openHTML() takes place in its own brackets not for any programmatic reason, but so that it's obvious that this is necessary regardless of what else goes on in the program. This is purely a matter of personal taste, so you can disregard the practice if you have other priorities.

The method c.getColor() will return the string blue, so the theme_name variable should inevitably be set to blue_theme.spi before we use it (the extension .spi is a convention for naming spyce include files, but you could also name this file .spy and it will work the same). Here are the contents of the file, blue_theme.spi, which we will dynamically include in our program:

File: blue_theme.spi

   class blueResponse:
      def returnColor(self):
         return('<br>Love is blue.<br>')

b = blueResponse() color = b.returnColor() print color ]] [[ print color ]]

I included the print color command twice, separated by brackets to illustrate that the contents of the variable color is persistent across brackets within the dynamically included file. However, as you'll see, the change in value of the variable color does not persist into the main file. I'll demonstrate this by setting color before the dynamic include, change it within the included file and then print it again after the include. In order to do a dynamic include, we have to import the spyce include module. That's why the [[.import]] directive appears in the code.

[[.import name=include]]
[[.include file=classes.spy]]
[[ openHTML() ]]
   c = themeColor()
   theme_name = c.getColor() + "_theme.spi"
   x = doHello()

color = '<br>Love is purple.<br>'

print color


print color ]] [[.include file=colorchange.spy]] [[ print color ]] [[ closeHTML() ]]

If you were paying attention, you probably noticed that I inserted yet another static include at the bottom of the main file. The contents of this included file, colorchange.spy are:

File: colorchange.spy [[ color='<br>Love is green.</br>' ]]

The program prints out the value of color once again after this static include. What you'll see when you load index.spy is that the value of color persists across the dynamically included file that also has the variable color and changes it. However, the value of color does not persist across the statically included file that attempts to change it, because that included file became part of the source code index.spy at compile time.

Here's the output you should see (excluding my comments in {} braces)

Hello world {output of the for loop from classes.spy}
Hello world
Hello world
Hello world
Hello world

Love is purple. {the value of color we set in index.spy}

Love is blue. {the value of color we set in the dynamically included blue_theme.spi}

Love is blue. {ditto, even across brackets}

Love is purple. {the value in index.spy is unchanged}

Love is green. {the value was changed by colorchange.spy}

There's a good reason why I chose to name the dynamically included file blue_theme.spi. Many PHP programmers use techniques like these to make their sites multi-theme-friendly. In other words, some PHP programmers are used to looking up a user's color preference and then using that to generate a file name at run-time, such as blue_vars.php or green_vars.php. These files contain different configuration settings your main program uses at run-time in order to set the colors for the theme. Obviously, this won't work with spyce dynamic includes because the main program won't "see" those settings in the dynamically included file.

It isn't impossible to accomplish the same basic task with spyce, but you simply have to keep in mind that whatever you do in the dynamically included file must be self-contained. Spyce programmers have yet another more-elegant alternative, which I'll cover in my next article.

More Stories By Nicholas Petreley

Nicholas Petreley is a computer consultant and author in Asheville, NC.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.

IoT & Smart Cities Stories
Moroccanoil®, the global leader in oil-infused beauty, is thrilled to announce the NEW Moroccanoil Color Depositing Masks, a collection of dual-benefit hair masks that deposit pure pigments while providing the treatment benefits of a deep conditioning mask. The collection consists of seven curated shades for commitment-free, beautifully-colored hair that looks and feels healthy.
The textured-hair category is inarguably the hottest in the haircare space today. This has been driven by the proliferation of founder brands started by curly and coily consumers and savvy consumers who increasingly want products specifically for their texture type. This trend is underscored by the latest insights from NaturallyCurly's 2018 TextureTrends report, released today. According to the 2018 TextureTrends Report, more than 80 percent of women with curly and coily hair say they purcha...
The textured-hair category is inarguably the hottest in the haircare space today. This has been driven by the proliferation of founder brands started by curly and coily consumers and savvy consumers who increasingly want products specifically for their texture type. This trend is underscored by the latest insights from NaturallyCurly's 2018 TextureTrends report, released today. According to the 2018 TextureTrends Report, more than 80 percent of women with curly and coily hair say they purcha...
We all love the many benefits of natural plant oils, used as a deap treatment before shampooing, at home or at the beach, but is there an all-in-one solution for everyday intensive nutrition and modern styling?I am passionate about the benefits of natural extracts with tried-and-tested results, which I have used to develop my own brand (lemon for its acid ph, wheat germ for its fortifying action…). I wanted a product which combined caring and styling effects, and which could be used after shampo...
The platform combines the strengths of Singtel's extensive, intelligent network capabilities with Microsoft's cloud expertise to create a unique solution that sets new standards for IoT applications," said Mr Diomedes Kastanis, Head of IoT at Singtel. "Our solution provides speed, transparency and flexibility, paving the way for a more pervasive use of IoT to accelerate enterprises' digitalisation efforts. AI-powered intelligent connectivity over Microsoft Azure will be the fastest connected pat...
There are many examples of disruption in consumer space – Uber disrupting the cab industry, Airbnb disrupting the hospitality industry and so on; but have you wondered who is disrupting support and operations? AISERA helps make businesses and customers successful by offering consumer-like user experience for support and operations. We have built the world’s first AI-driven IT / HR / Cloud / Customer Support and Operations solution.
Codete accelerates their clients growth through technological expertise and experience. Codite team works with organizations to meet the challenges that digitalization presents. Their clients include digital start-ups as well as established enterprises in the IT industry. To stay competitive in a highly innovative IT industry, strong R&D departments and bold spin-off initiatives is a must. Codete Data Science and Software Architects teams help corporate clients to stay up to date with the mod...
At CloudEXPO Silicon Valley, June 24-26, 2019, Digital Transformation (DX) is a major focus with expanded DevOpsSUMMIT and FinTechEXPO programs within the DXWorldEXPO agenda. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive over the long term. A total of 88% of Fortune 500 companies from a generation ago are now out of business. Only 12% still survive. Similar percentages are found throug...
Druva is the global leader in Cloud Data Protection and Management, delivering the industry's first data management-as-a-service solution that aggregates data from endpoints, servers and cloud applications and leverages the public cloud to offer a single pane of glass to enable data protection, governance and intelligence-dramatically increasing the availability and visibility of business critical information, while reducing the risk, cost and complexity of managing and protecting it. Druva's...
BMC has unmatched experience in IT management, supporting 92 of the Forbes Global 100, and earning recognition as an ITSM Gartner Magic Quadrant Leader for five years running. Our solutions offer speed, agility, and efficiency to tackle business challenges in the areas of service management, automation, operations, and the mainframe.