Author Archives: Akako L

Real-world example for Julia multidispatch (where using python is a pain)

Real-world example for Julia multidispatch (where using python is a pain)

Picture by me of the blood moon!


This quarter I’m taking a physics/programming class where we learn numerical methods in the context of physics(high energy more or less) with python. We’ve covered some interesting tools and numerical approaches such as Markov Chain Monte Carlo, Bayesian inference etc.

Naturally, we would use numpy in python extensively since the vectorized nature makes it very easy to manipulate arrays. To people who’re less familiar, this is what I mean:

import numpy as np
xs = np.linspace(1,10,10) #[1,2,3,...,10]
ys = np.power(x,2) #[1,4,9....,100]

The problem occurs when a numpy function can handle both number and array (also overide*). Continuing the example above, now image I have a function:

def integrate(u):
    ary = np.exp(-ys) + u # this is array because ys is array
    return np.sum(ary) # this will sum the above array
    
s = [integrate(x) for x in xs]
s = integrate(xs) # these two will yield different result

This is because when passing xs to integrate, python has no way of knowing if I wans it to treat it as int or array. Of course, this is not too nasty because expand list comprehension is easy. But it took me too long to realize this is the bug.

In contrast, in julia I could have made clear in the definiton of the integrate(u) for example:

function integrate(u::Number)
    ...
end

Initially I tried to google how to force typing in python and then realize that’s the opposite of python philosophy. This is also reflected in the ugly __mul__(self, other) magic function in making a class in python. In that case, you would have to write multiple if isinstance(other, <something>): clauses to make things workd, where in julia I could have the blessed super/sub type to figure out what method to use when executing the function.

A telegram bot in Julia

A telegram bot in Julia

I just want to see source code.


Fetching and replying of telegram bot messages can be handle by HTTP.jl easily, unless you forget to escape uri with percentages. Because the main use case is to respond to commands in telegram, I used a Dict() approach where we give the bot a dictionary of functions so that whenever it sees matching commands, it will reply with the result of that function (given parameter).

using telegram

botApi = "<your_api>"

function echo(incoming::AbstractString)
    return incoming
end

txtCmds = Dict()
txtCmds["repeat_msg"] = echo #this will respond to '/repeat_msg <any thing>'
txtCmds["start"] = x -> "Welcome to my bot" # this will respond to '/start'

telegram.startBot(botApi; textHandle = txtCmds)

The inline mode is still missing, any help is appreciated!
Thx to the PR from simeonschaub, now that the bot has an inline mode! The example code is shown below, where I modified upon their PR to make the invoking style matching.

using Telegrambot
using UUIDs
botApi = "bot<your_api_key>"

function welcomeMsg(incoming::AbstractString)
    return "Welcome to my awesome bot"
end

function echo(incoming::AbstractString)
    return incoming
end

txtCmds = Dict()
txtCmds["repeat_msg"] = echo #this will respond to '/repeat_msg <any thing>'
txtCmds["start"] = welcomeMsg # this will respond to '/start'

inlineOpts = Dict() #Title, result pair
inlineOpts["Make Uppercase"] = uppercase #this will generate an pop-up named Make Uppercase and upon tapping return uppercase(<user_input>)

#uppercase is a function that takes a string and return the uppercase version of that string

startBot(botApi; textHandle = txtCmds, inlineQueryHandle=inlineOpts)

How to make package and set up Travis for Julia 1.0

How to make package and set up Travis for Julia 1.0

Cover


Travis-CI provides free build check for public project, it’s perfect for individual to put a little nice build badge on README.md inside the repository. It also has a paid plan for larger collaboration that needs to run build check for every pull requests and different branches where they will run several builds in parallel. (Also this is because larger project such as Julialang itself, can also take more than 4 hours to even build once).

But smaller, personal projects usually take less than an hour to build thus takes no cost.
How to make package and set up Travis for Julia 1.0

A very basic Julia package should have the following structure:

.
├── LICENSE
├── Manifest.toml
├── Project.toml
├── README.md
├── REQUIRE
├── src
│   └── scryfall.jl
└── test
    └── runtests.jl

Source code in src directory should have something structure as:

module <pkg_name>
using <dependent_pkg>

export foo

function foo
#do something
end

end #end of module

Where the Manifest.toml and Project.toml should be auto generated by

(v1.0) Pkg> generate <pakcage_name>

Afterwards, cd into the directory, and then issue

(v1.0) Pkg> activate .

to activate the package. After this, anything you add as a dependent with add <pkg_name> will be automatically updated into Project.toml.


Once you’be done making the package, it’s time to deploy Travis-CI, for the purpose julia, you will need a .travis.toml file in the base directory of github repo that looks like this:

language: julia
os:
  - linux
julia:
  - nightly
  - 1.0

Then, in order for Travis-CI to catch tests, you will want a test folder with a runtest.jl inside, where Travis-CI will examine the results after building your packages. (Don’t forget to include Test as a julia dependent in Project.toml). In the runtests.jl , you will want to issue using <your_package> and using Test then use @test macro to test some true-false statement, if all goes well, you Travis-CI will spit out a success result.

How to make package and set up Travis for Julia 1.0