https://github.com/andrewrgarcia/bridge
Connecting arrays between Python and C++
https://github.com/andrewrgarcia/bridge
array bridge cpp hacktoberfest json matrix port programming-languages python tensor transformation translation
Last synced: 3 months ago
JSON representation
Connecting arrays between Python and C++
- Host: GitHub
- URL: https://github.com/andrewrgarcia/bridge
- Owner: andrewrgarcia
- License: mit
- Created: 2022-07-16T20:07:50.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-10-14T23:49:21.000Z (over 3 years ago)
- Last Synced: 2025-10-31T10:45:04.467Z (8 months ago)
- Topics: array, bridge, cpp, hacktoberfest, json, matrix, port, programming-languages, python, tensor, transformation, translation
- Language: C++
- Homepage:
- Size: 131 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# bridge

This is an open-source suite of mini-libraries designed to connect array data between Python and C++ through the use of JSON files.
Python and C++ modules use **tojson** and **jsonload** functions to convert [one- two- and three-dimensional] arrays to .json DOK (Dictionary of Keys) and viceversa, respectively. See Usage Examples below.
## Contributions Welcome / Hacktoberfest
Meaningful contributions to the project are always welcome. Participating in Hacktoberfest 2022. Before making a PR, please make sure to read the [CONTRIBUTING](./CONTRIBUTING.md) document.
You may use the Issues section of this repository if you'd like to propose some new ideas/enhancements or report a bug.
## Installation / Setup
### Dependencies
#### [JSON for Modern C++](https://github.com/nlohmann/json)
```ruby
# Download JSON repository
git clone https://github.com/nlohmann/json.git
# Access it
cd json
#copy as system variable
sudo cp -R include/nlohmann/ /usr/include/nlohmann
```
### Bridge suite
```ruby
# Download this repository
git clone https://github.com/andrewrgarcia/bridge.git
# Access it
cd bridge
#copy as system variable to call bridge.cpp from anywhere
sudo cp -R include/garcia/ /usr/include/garcia
# Get Python mini-library ("ponte")
pip install ponte
```
## Usage Examples
**C++**
```ruby
#include
#include
#include
#include
int main()
{
// CREATE A VECTORIZED ARRAY
std::vector vector0(8, 1);
std::vector DIMS = {2, 2, 2};
std::string filename{"sample.json"};
// pack vector to .json file
tojson(filename, vector0, DIMS);
// PROCESS JSON FILE BACK TO VECTOR
std::vector vector = jsonload(filename);
// print vector
std::cout << "\nvectorized array: " << std::endl;
for (int i = 0; i < vector.size(); i++)
{
std::cout << vector[i] << " ";
}
std::cout << std::endl;
}
```
**Python**
```python
import ponte as bridge
import numpy as np
'CREATE A 1-D VECTOR'
X = np.random.choice([0,1,2,3],10,p=[0.7,0.1,0.1,0.1])
'pack vector to .json file'
bridge.tojson('sample.json',X)
'PROCESS JSON FILE BACK TO VECTOR'
array_form = bridge.jsonload('sample.json')
'print vector'
print('array:\n',array_form)
```
## Sample Output
```ruby
array to DOK:
{
"map": [
[1],
[4],
[5],
[8],
[9]
],
"value": [2, 3, 2, 2, 2],
"odims": [10]
}
DOK to array:
[0. 2. 0. 0. 3. 2. 0. 0. 2. 2.]
```