Use Python package collections in Ruby app
This article provides an introduction to cross-technology handling of collections. Data structures are one of the essential aspects of every piece of software. Any application constantly process various information, that very often require specific grouping and access strategies. This aspect is addressed by arrays and more advanced collection types. By using the Javonet framework, users gain ability to easily and effectively work with data structures originating from Python package. Every collection from Python package is treated as reference.
Javonet allows you to reference and use modules or packages written in (Java/Kotlin/Groovy/Clojure, C#/VB.NET, Ruby, Perl, Python, JavaScript/TypeScript) like they were created in your technology. If have not yet created your first project check Javonet overview and quick start guides for your technology.
With Javonet you can interact with collections from Python package like they were available in Ruby but invocation must be performed through Javonet SDK API.
Work with Python package lists in Ruby application
Javonet SDK contains various methods to interact with lists and consume the results in Ruby:
# use activate only once in your app
Javonet.activate('your-license-key')
# create called runtime context
called_runtime = Javonet.in_memory.python
# get list from built-in types
type_list = called_runtime.get_type('builtins.list').execute
# create instance of list
list = type_list.create_instance.execute
# invoke instance method
list.invoke_instance_method('extend', %w[one two three four five six]).execute
# get elements from list
element0 = list[0].execute
element1 = list.get_index(1).execute
result0 = element0.get_value
result1 = element1.get_value
# get size of list
size = list.get_size.execute.get_value
# write results to console
puts result0
puts result1
puts size
In the snippet above, list specific to called technology framework is created.
Then, some elements are added to the list.
Finally, this list can be handled through Javonet SDK, f.e. with getIndex method.
Work with Python package dictionaries in Ruby application
Javonet SDK contains various methods to interact with dictionaries and consume the results in Ruby:
# use activate only once in your app
Javonet.activate("your-license-key")
# create called runtime context
called_runtime = Javonet.in_memory.python
# get generic class
type_dictionary = called_runtime.get_type("builtins.dict").execute
# create instance of generic class
dictionary = type_dictionary.create_instance.execute
# set elements in dictionary
dictionary.set_index("pi", Math::PI).execute
dictionary.set_index("e", Math::E).execute
dictionary.set_index("c", 299792458.0).execute
# get elements from dictionary
response1 = dictionary["c"].execute
response2 = dictionary.get_index("e").execute
c_value = response1.get_value
e_value = response2.get_value
# write results to console
puts c_value
puts e_value
In the snippet above, list specific to called technology framework is created.
Then, some elements are added to the list.
Finally, this list can be handled through Javonet SDK, f.e. with getIndex method.
The same operation can be performed remotely by just changing the new Runtime Context invocation from in memory to tcp that will create and interact with your Python package objects on any remote node, container or service that hosts Javonet Code Gateway. This way you can preserve the same logic in your application and instantly switch between monolithic and microservices architecture without the need to implement the integration layer based on web services or other remote invocation methods.
Read more about use cases and software architecture scenarios where Javonet runtime bridging technology can support your development process.
Was this article helpful?