Skip to content

Types

Mpl supports commonly used types in the targetted application as language builtins. These natively supported types help reduce Mpl code's complexity and keep the code bloat in check. Each built-in types support convenience methods to accomplish various transformation tasks. e.g., x.lower() returns the lowercase value of the string, x.

String

A string is single or double quoted variable. e.g., 'blue', "green", with escape sequence rules in common languages.

Methods:

  1. lower: lower returns lowercase value of string.

    mpl> x = "Apple"
    mpl> x.lower()
    "apple"
    
  2. upper: upper returns uppercase value of string.

    mpl> x = "Apple"
    mpl> x.upper()
    "APPLE"
    
  3. isalnum: isalnum returns 'true' if string is alphanumeric.

    mpl> x = "1a"
    mpl> x.isalnum()
    true
    
  4. isalpha: isalpha returns 'true' if string contains of alphabets only.

    mpl> x = "aa"
    mpl> x.isalpha()
    true
    
  5. isdigit: isdigit returns 'true' if string is digit only.

    mpl> x = "11"
    mpl> x.isdigit()
    true
    
  6. isdecimal: isdecimal returns 'true' if all characters of string is a number

    mpl> x = "11.0"
    mpl> x.isdecimal()
    false
    
  7. isupper: isupper returns 'true' if string is uppercase.

    mpl> x = "aaA"
    mpl> x.isupper()
    false
    
  8. islower: islower returns 'true' if string is lowercase.

    mpl> x = "aab"
    mpl> x.islower()
    true
    
  9. isspace: isspace returns 'true' if string is composed of space characters only.

    mpl> x = "aab"
    mpl> x.isspace()
    true
    
  10. startswith(str): startswith returns 'true' if the string starts with provided string arg.

    mpl> x = "AA1aa"
    mpl> x.startswith("AA")
    true
    
  11. endswith(str): startswith returns 'true' if the string ends with provided string arg.

    mpl> x = "AA1aa"
    mpl> x.endswith("a")
    true
    
  12. count(str): count counts the number of non-overlapping instances of substr in s.

    mpl> x = "AA1aa"
    mpl> x.count("a")
    2
    
  13. find(str): find returns the index of the first instance of substr in s, or -1 if substr is not present in s.

    mpl> x = "AA1aa"
    mpl> x.find("a")
    3
    
  14. rfind(str): rfind returns the index of the last instance of substr in s, or -1 if substr is not present in s.

    mpl> x = "AA1aa"
    mpl> x.rfind("a")
    0
    
  15. lstrip(str): lstrip returns a copy of the string with leading characters removed. Argument is a string specifying the set of characters to be removed

    mpl> x = "AA1aa"
    mpl> x.lstrip("A")
    "1aa"
    
  16. rstrip(str): rstrip return a copy of the string with trailing characters removed. Argument is a string specifying the set of characters to be removed

    mpl> x = "AA1aa"
    mpl> x.rstrip("a1")
    "AA"
    
  17. strip(str): strip return a copy of the string with leading and trailing characters removed. Argument is a string specifying the set of characters to be removed

    mpl> x = "AA1aa"
    mpl> x.strip("aA")
    "1"
    
  18. title: title returns a copy of the string s with all letters that begin words changed to uppercase.

    mpl> x = "apple pie"
    mpl> x.title()
    "Apple Pie"
    
  19. replace(old, new): Return a copy of the string with all occurrences of substring old replaced by new.

    mpl> x = "AA1aa"
    mpl> x.replace("a", "b")
    "AA1bb"
    
  20. split: Splits the string at the specified separator, and returns a list. It takes an optional integer arg to specify max number of split.

    mpl> x = "demo.acme.com"
    mpl> x.split(".")
    ["demo","acme","com"]
    mpl> x.split(".")
    ["demo","acme.com"]
    
  21. join: Join concatenates the elements of first Json array argument to create a single string. The separator string sep is placed between elements in the resulting string.

    mpl> x = "AA1aa"
    mpl> x.find("a")
    3
    
  22. time: time tries to parse string to detect time. If the string is in one of the supported time formats, it returns a time object. This method is useful detecting time string in Json documents. e.g.,

    mpl> x = "2021-02-12T00:48:06.825Z"
    mpl> x.time()
    02-12-2021 12:48:06 am UTC
    
  23. duration: duration parses string and converts it to object type duration. Duration string should be in 'hⓂs' format. Commonly used phrases like "1 hour 1 min" is also supported.

    mpl> "2h:5m:5s".duration()
    2h:5m:5s
    mpl> s = "1 hour 1 min"
    mpl> s.duration()
    1h:1m:0s
    
  24. num: num tries to parse string to a num object. String can be a integer or float. If string is not parsable to number, an error is returned. e.g.,

    mpl> x = "-2.0"
    mpl> x.num() + 3
    1
    
    mpl> x = "a1"
    mpl> x.num()
    String 'a1' is not convertable to number type.
    

Number

A number type can be integer or float.

Methods:

  1. string: string returns string representation of number.

    mpl> i = 10
    mpl> i.string()
    "1"
    
  2. duration: duration converts a number into duration object. The unit of number can be specified as string argumeent. If arg is not specified, "sec" is assumed. Valid arg values are "hour", "hr", "min:, "ms", and "ns". e.g.,

    mpl> x = 5.20 
    mpl> x.duration("hr")
    5h:12m:0s
    

Boolean

Boolean type represents 'true' or 'false'.

Methods:

  1. string: string returns string representation of boolean.

    mpl> i = false
    mpl> i.string()
    "false"
    

Time

Time is a builtin type in Mpl. Mpl parser recongnizes user friendly time statement. time is Mpl builtin which translates the subsequent phrase into time. e.g.,

x = time 10-11-2010 06:11:00 pm PDT
y = time 10-11-2010
print x
print y

output is:

10-11-2010 06:11:00 pm PDT
10-11-2010 12:00:00 am UTC

Apart from above 'Mpl time format' Mpl also suports commonly used standard formats. These formats can be input as string. Mpl detects the time format automatically by walking through supported format strings and creating a time object internally. e.g.,

mpl> x = time ("2021-02-12T00:48:06.825Z")
x

output is:

02-12-2021 12:48:06 am UTC

Methods:

  1. second: second returns number of seconds.

    mpl> x = time ("2021-02-12T00:48:06.825Z")
    mpl> x.second()
    6
    
  2. minute: minute returns number of minutes.

    mpl> x = time ("2021-02-12T00:48:06.825Z")
    mpl> x.minute()
    48
    
  3. hour: hour returns number of hours.

    mpl> x = time ("2021-02-12T00:48:06.825Z")
    mpl> x.hour()
    0
    
  4. day: day returns number of days.

    mpl> x = time ("2021-02-12T00:48:06.825Z")
    mpl> x.day()
    12
    
  5. month: month returns number of months.

    mpl> x = time ("2021-02-12T00:48:06.825Z")
    mpl> x.month()
    2
    
  6. year: year returns number of years.

    mpl> x = time ("2021-02-12T00:48:06.825Z")
    mpl> x.year()
    2021
    
  7. utc: utc returns time with the location set to UTC.

    mpl> x = time ("2021-02-12T00:48:06.825Z")
    mpl> x.utc()
    02-12-2021 12:48:06 am UTC
    
  8. string([format]): string converts time in desired format. The desired format can be specified optionally . Valid format strings are: "RFC3339", "UnixDate", "RFC1123", "RFC822Z", "RubyDate", "ANSIC", "RFC850", "Kitchen", and "epoch". If format is not specified, it defaults to Mpl format.(e.g., 10-11-2010 06:11:00 pm PDT).

    mpl> x = time 10-21-2010 06:11:00 pm PDT
    mpl> y = x.string("RFC3339")
    mpl> y
    "2010-10-21T18:11:00-07:00"
    
  9. print: print is same as string method, just that it prints the value instead of returning formatted string.

Duration

Duration builtin type represents span of time. Difference between 2 time object is of type duration. duration is Mpl builtin which translates the subsequent phrase into duration. e.g., duration 1 hour. Putting 1 hour in bracket or quotes is optional. Time object can be added or subtracted with duration. e.g.,

x = duration 1 day
print x
y = time 10-19-2011 06:11 pm PDT - x
print y

output is:

24h:0m:0s
10-18-2011 06:11:00 pm PDT

Keywords to specify duration are, 'year', 'month', 'day', 'hour', 'minute', 'second'. Any comnbination of these are valid just that the sequence must be in the order. e.g., 'minute' cannot come before 'hour'. 'Hour', 'minute' and 'second' can also be sepecified as 'h', 'm' and 's' respectively.

Methods:

  1. seconds: seconds returns duration in number of seconds

    mpl> x = duration 1 day 
    mpl> x.seconds()
    86400
    
  2. minutes: minutes returns duration in number of minutes

    mpl> x = duration 1 day 
    mpl> x.minutes()
    1440
    
  3. hours: hours returns duration in number of hours

    mpl> x = duration 1 day 
    mpl> x.hours()
    24
    
  4. string: string returns duration in human readable format

    mpl> x = duration 1 day 5 m
    mpl> x.string()
    "24h:5m:0s"
    

Json

Json type is the main buitlin type supported by Mpl. Most of the workflow logic revolves around commands and its result in the form of Json a document. Mpl comes with rich set of feature to query and process these Json document. A Json type can be created by a keyword json followed by a valid Json syntax. In the most basic form a value in Json document can be accessed or set like a Python array and map.

Indexed variable: For accessing key value pair, dotted notation is easy and is preferred. e.g.,

x = json {"color": ["red", "blue"]}
x.color[1]

output is:

"blue"

Alternatively the same value can be accessed as x["color"][1]. Dotted notation is more convenient in most of the cases unless we want to access map via a Mpl vairable. e.g.,

c = "color"
x = json {"color": ["red", "blue"]}
x[c][1]

Setter: A Json value can be set similarly as:

x = json {"color": ["red", "blue"]}
x.color[1] = "purple"
x

output is:

{"color":["red","purple"]}

Mpl Json statement can also take a variable for any Json field. These variables will be resolved by Mpl, before creating the Json object. e.g.,

c = "purple"
k = "color"
x = json {k: ["red", c]}
x

output is:

{"color":["red","purple"]}

Methods:

  1. append: append method appends an element to the end of the list.

    mpl> x = json {"a":[1, 5, 2]}
    mpl> x.a.append(5)
    mpl> x
    {"a":[1,5,2,5]}
    
  2. clear: clear method removes all the elements from a list.

    mpl> x = json {"a":[1, 5, 2]}
    mpl> x.a.clear()
    mpl> x
    {"a":[]}
    
  3. copy: copy method returns a copy of the specified list.

    mpl> x = json {"a":[1, 5, 2]}
    mpl> x = json[1,2]
    mpl> y = x.copy()
    mpl> x.append(2)
    [1,2,2]
    mpl> y
    [1,2]
    
  4. count(value): count method returns the number of elements with the specified value in a list.

    mpl> x = json {"a":[1, 5, 2]}
    mpl> x.a.count(5)
    1
    
  5. extend: extend method adds the specified list elements to the end of the current list.

    mpl> y = json {"a":[1, 5, 2, "a", "b"]}
    mpl> x = json [7, 9, "aGB", true]
    mpl> r = y.a.extend(x)
    mpl> y
    {"a":[1,5,2,"a","b",7,9,"aGB",true]}
    
  6. get(string, [number|string|bool]): get method returns the value of the item with the specified key. If key is not found, default value is returned. e.g.,

    mpl> y = json {"a":1, "b": "aa"}
    mpl> y.get("b", "not found")
    "aa"
    
  7. index: index method returns the position at the first occurrence of the specified value.

    mpl> y = json {"a":[1, 5, 2, true]}
    mpl> y.a.index(5) + y.a.index(true)
    4
    
  8. insert: insert method inserts the specified value at the specified position

    mpl> y = json {"a":[1, 5, 2]}   
    mpl> z = y.a.insert(1, 3)
    mpl> l= z.insert(100, 9)
    mpl> l
    [1,3,5,2,9]
    
  9. keys: keys method returns json list containing the keys of the json map.

    mpl> y = json {"a":1, "b": 2}
    mpl> y.keys()
    ["a","b"]
    
  10. pop: pop method removes the element at the specified position.

    mpl> y = json {"a":[1, 5, 2]}
    mpl> y.a.pop(1)
    mpl> y
    {"a":[1,2]}
    
  11. remove: remove method deletes the first occurrence of the element with the specified value. e.g.,

    mpl> y = json {"a":[1, 5, 5, 2, "a"]}
    mpl> y.a.remove(5)
    mpl> y.a.remove("a")
    mpl> y
    {"a":[1,5,2]} 
    
  12. reverse: reverse method reverses the sorting order of the elements.

    mpl> y = json {"a":[1, 5, 2, "a", "b"]}
    mpl> r = y.a.reverse() 
    mpl> y
    {"a":["b","a",2,5,1]}
    
  13. string: string method returns string representation of Json value.

  14. sort([boolean], [stirng]): sort returns sorted copy of Json. if reverse arg sorts the array in descending order. If key is specified sort is done on array of maps based on value of specified key. e.g.,

    mpl> x = json [1,3,2]
    mpl> x.sort()
    [1,2,3]
    

    x = json [{"a":1}, {"a":3}, {"a",2}]
    x.sort(true, "a")
    
    output is:

    [{"a":3},{"a":2},{"a":1}]
    
  15. update: update method inserts the specified items to the dictionary. It takes a json map as input.

    mpl> y = json {"a":1, "b": "aa"}
    mpl> x = json {"c": 2, "d": "bb"}
    mpl> y.update(x)
    {"a":1,"b":"aa","c":2,"d":"bb"}
    
  16. values: values method returns json list containing the values of the json map.

    mpl> y = json {"a":1, "b": "aaC", "c": true}
    mpl> y.values()
    [1,"aaC",true]
    

  17. to_keyval: to_keyval returns a new Json with map entries changed to array of tuple, "key":<key> & "value":<value>. This is useful if one wants to access the "key" of the Json map. e.g.,

    y = json {"a":1, "b":2}
    y.to_keyval()
    

    output is:

    [{"key":"a","value":1},{"key":"b","value":2}]
    
    x = json [{"n1":1}, {"n2":2}]
    x[].to_keyval()
    

    output is a Json list:

    {"key":"n1","value":1},{"key":"n2","value":2}
    

    To get Json list of all keys.

    x = json [{"n1":1}, {"n2":2}]
    y = x[].to_keyval()
    y[].key
    

    output is:

    ["n1", "n2"]
    
  18. from_keyval: from_keyval is complementary functon of to_keyval(). It converts the Json output of to_keyval() back to the original format. e.g.,

    p = json [{"key":"a","value":1},{"key":"b","value":2}] 
    p.from_keyval()
    

    output is:

    {"a":1,"b":2}
    

    similarly:

    x = json [{"n1":1}, {"n2":2}]
    a = x[].to_keyval()
    a.from_keyval()
    

    output is:

    {"n1":1,"n2":2}