Skip to main content

No project description provided

Project description

C-Py-Py

from cpp import *

x = (cpp)[std::vector<int>v({1, 2, 3})]
x.push_back(4)
(cpp)[std::cout] << "Vector x: " << x << (cpp)[std::endl]
# -> prints 'Vector x: [1, 2, 3]'
for i in auto& x:
    (cpp)[std::cout] << "Incrementing " << i << "..." << (cpp)[std::endl]
    # -> prints 'Incrementing 1...', 'Incrementing 2...', etc.
    i += 1

(cpp)[std::cout] << "Vector after: " << x << (cpp)[std::endl]
# -> prints 'Vector after: [2, 3, 4, 5]'

How?

Template notation

The <> template notation was quite difficult to pull off. Python has a weird concept of multiple-boolean-operators, so the following:

x = (cpp)[std::vector<int>v({1, 2, 3})]

is equivalent to

x = (cpp)[std::((vector < int) and (int > v({1, 2, 3})))]

We can then overwrite the less than operator for the object vector to simply return True, so that it's negligible:

x = (cpp)[std::(True and (int > v({1, 2, 3})))]
x = (cpp)[std::(int > v({1, 2, 3}))]

Now we can overwrite the less than operator on a different class (v, in this case) so that it simply takes in a fully formed vector object as self and a type as the comparison, then tries to transform that into a new vector of the type in the comparison. This would be equivalent to:

x = (cpp)[std::(v({1, 2, 3}))]

C++-style namespacing

Nearly there. For the namespacing (::), we turn to the only place in the Python syntax where adjacent colons are allowed: slice notation. The code above is equivalent to:

x = cpp[slice(std, None, v({1, 2, 3}))]

We can define cpp to be an instance of a class that overrides the __getitem__ method to simply return the rightmost part of the slice:

class cpp:
    def __getitem__(self, other: slice):
        return other.step
cpp = cpp()

Now the code is equivalent to just:

x = v({1, 2, 3})

where v is essentially a thin wrapper around list.

cout

cout performs a small sleight-of-hand. Since Python is evaluated left-to-right, we have to have the << operator reduce each of the expressions down into a single format string, then pass that to endl to actually do the printing. We do this by making cout.lshift() return a Coutable:

class _Coutable:
    def __init__(self, o) -> None:
        self._total_str: str = format(o)

    def __lshift__(self, other: Any) -> Self:
        if other is endl:
            print(self._total_str)
        self._total_str = self._total_str + format(other)
        return self

This class will just keep accumulating objects' formatted representations until it hits endl, when it will print everything out.

Taking references

Unfortunately, Python's for _ in _: syntax is pretty rigid, and won't allow any operations in-between for and in, so we have to stick the auto& on the right side. This

Why?

Scientists are hard at work trying to come up with an answer to that question.

Project details


Download files

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

Source Distribution

c-py-py-0.1.5.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

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

c_py_py-0.1.5-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file c-py-py-0.1.5.tar.gz.

File metadata

  • Download URL: c-py-py-0.1.5.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.11.0b3 Windows/10

File hashes

Hashes for c-py-py-0.1.5.tar.gz
Algorithm Hash digest
SHA256 908d8aabe911e184622e7babf764bb3ce9e78dcd5c424102612896da6e6bfb0b
MD5 f5349c86bd3090330a453e2393bc2dd3
BLAKE2b-256 368961b71039022199f43aa2166c8ed559716259d160b0733d134b8b6774e8fc

See more details on using hashes here.

File details

Details for the file c_py_py-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: c_py_py-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 3.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.11.0b3 Windows/10

File hashes

Hashes for c_py_py-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 633c0837523c0a881e2fee039aec2909c2b7e9faa9589b74e0726482d8bdc0ad
MD5 06b84d79795cd43b80af463fcd9102dc
BLAKE2b-256 4d2bc5a28277d28fabdd211d6163bddad057e840a0256e5f222e564927268c8d

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