Skip to main content

用python写c

Project description

将python函数翻译为c++函数并运行

hello_world.py

import l0n0lc as lc
import math


@lc.映射函数(math.ceil, ['<cmath>'])
def cpp_ceil(v):
    return f'std::ceil({lc.toCString(v)});'


@lc.映射函数(print, ['<iostream>'])
def cpp_cout(*args):
    code = f'std::cout'
    for arg in args:
        code += f'<< {lc.toCString(arg)} << " "'
    code += '<< std::endl;'
    return code

@lc.直接调用函数
def test_直接调用():
    return 123

@lc.jit(每次运行都重新编译=True)
def test_add(a: int, b: int) -> int:
    if a > 1:
        return a + b
    for i in range(1, 10, 2):
        a += i
    for i in [1, 2, 3]:
        a += i
    a = math.ceil(12.5)
    cc = {'a': 1, 'b': 2}
    cc['c'] = 3
    print('输出map:')
    for ii in cc:
        print(ii.first, ii.second) # type: ignore
    aa = [1, 3, 2]
    aa[0] = 134
    print('输出list:')
    for i in range(3):
        print(i, aa[i])
    print('Hello World', a, b)
    return a + b + 1 + test_直接调用()

print('结果:', test_add(1, 3))

执行hello_world.py

$ python hello_world.py
输出map: 
c 3 
a 1 
b 2 
输出list: 
0 134 
1 3 
2 2 
Hello World 13 3 
结果: 140

查看生成的c++代码文件

$ ls -al l0n0lcoutput/ 
total 48
drwxr-xr-x  2 root root  4096 Sep 11 02:32 .
drwxrwxrwx 11 1000 1000  4096 Sep 11 02:32 ..
-rw-r--r--  1 root root   794 Sep 11 02:32 test_add_@0bfbc28de3631ddd.cpp
-rw-r--r--  1 root root   150 Sep 11 02:32 test_add_@0bfbc28de3631ddd.h
-rwxr-xr-x  1 root root 29496 Sep 11 02:32 test_add_@0bfbc28de3631ddd.so

test_add_@0bfbc28de3631ddd.h

#include <cmath>
#include <cstdint>
#include <iostream>
#include <string>
#include <unordered_map>
extern "C" int64_t test_add (int64_t a, int64_t b);

test_add_@0bfbc28de3631ddd.cpp

#include "test_add_@141921ba1aaa0352.h"
extern "C" int64_t test_add (int64_t a, int64_t b)
{
  if ((a > 1))
  {
    return a + b;
  }

  for (int64_t i = 1; i < 10; i += 2)
  {
    a = a + i;
  }

  for (auto i : {1,2,3})
  {
    a = a + i;
  }

  a = std::ceil(12.5);;
  std::unordered_map<std::string, int64_t> cc = {{ u8"a", 1 },{ u8"b", 2 }};
  cc[u8"c"] = 3;
  std::cout<< u8"输出map:" << " "<< std::endl;
  for (auto ii : cc)
  {
    std::cout<< ii.first << " "<< ii.second << " "<< std::endl;
  }

  int64_t aa[] = {1,3,2};
  aa[0] = 134;
  std::cout<< u8"输出list:" << " "<< std::endl;
  for (int64_t i = 0; i < 3; ++i)
  {
    std::cout<< i << " "<< aa[i] << " "<< std::endl;
  }

  std::cout<< u8"Hello World" << " "<< a << " "<< b << " "<< std::endl;
  return a + b + 1;
}

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

l0n0lc-0.7.3.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

l0n0lc-0.7.3-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

Details for the file l0n0lc-0.7.3.tar.gz.

File metadata

  • Download URL: l0n0lc-0.7.3.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for l0n0lc-0.7.3.tar.gz
Algorithm Hash digest
SHA256 13be13b36983f783809f3134a849d89ab2b7782868c6b465f0cce95fada3d4ac
MD5 b91df83b2c3316d727812d78568ee3fe
BLAKE2b-256 7ceadbe1bf5a61bcae0e94206b4eef231c130825f32feb0cbc7f502c24d97f66

See more details on using hashes here.

File details

Details for the file l0n0lc-0.7.3-py3-none-any.whl.

File metadata

  • Download URL: l0n0lc-0.7.3-py3-none-any.whl
  • Upload date:
  • Size: 13.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for l0n0lc-0.7.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c8c592efe2bdcaff21d49eda0e2130c6813dbbe309323eea68d907f47540b6df
MD5 4fc1530f574ca7bafb627e77f470b46f
BLAKE2b-256 2877cf902ad84d97a2f2cdab5c38bd102a8a5e3a89afbb3a452b145b549d862a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page