Ready to Start Your Career?

Part 2: Creating Modules for Juliar in Python

Rattar 's profile image

By: Rattar

September 3, 2016

Juliar-python
[clear][divider]This tutorial is continuation of Part 1: Creating Juliar Modules in Python.If you have no idea what's going on check out the previous part.https://www.cybrary.it/0p3n/part-1-creating-juliar-modules-python/
Pre-requisites:
  • You must have read first part of tutorial
  • You must have PythonDevKit downloaded
  • You must be able to work your brain

In this tutorial we will learn about optionals and why and how they are supposed to be used.Remember from the first tutorial: each command requires def name(content, optionals):Well optionals are optionals, meaning that the user of module can specify any amount of optionals, orthe user can specify none. Either way, you must be able to figure out what the optionals areand what to do with them.In *Juliar * optionals are specified:*commandname=optional_1,optional_2  content *where optional_1 and optional_2 are optionals.Again, user can specify 0 optionals or 20 optionals.You, as a module developer, is responsible for handling such cases.Let's create a new module and call it "<b>hello.py</b>"Let's open it and copy the following:def hello(content,optionals):return "I am "+content+" and my backpack contains "+optionals[0]Let's import it into Juliar by doing *import=py hello.py *Let's test it by doing *hello=pencil Andy *It will output "I am Andy and my backpack contains pencil"Let's test it again by doing *hello Andy *Oops...our program crashed (in the newer version this may just display a module error).Why did this happen?Well if you look at our command. We are using optionals. However, as we said before:Optionals are Optional. So they might or might not be used.

Let's fix it by checking size of optionals.Let's add this check to our current commanddef hello(content,optionals):
        if(len(optionals) == 0):
                return "I am "+content
        return "I am "+content+" and my backpack contains "+optionals[0]
        
By checking the length, we make sure that if user has not specified any optionals they would not experience a crash or an error.If you are not using optionals, then you must still keep optionals in the def, but there is no need to check for size.Congratulations, you just got a yellow belt in creating *Juliar * modules in python.More tutorials coming soon!
Schedule Demo