c-preprocessor

python library for metaprogramming like it's the 1970s
Log | Files | Refs | README

README.md (1635B)


      1 # c-preprocessor
      2 
      3 Ever felt like your Python code wasn't hacky enough? Lamented the bygone
      4 `#ifdef`s and cursed macro trickery? This is the Python package for you!
      5 
      6 Just by simply adding a single comment to the top of your file, you too can use
      7 a crude approximation of the C preprocessor from Python.
      8 
      9 ```py
     10 # coding: c-prepocessor
     11 
     12 #define TESTING 1
     13 #define DOUBLE(x) (x*2)
     14 #define MAX(x,y) x if x > y else y
     15 #define BLAH lots of words
     16 
     17 def myfunc():
     18     return TESTING
     19 
     20 #ifdef TESTING
     21 def debug_func():
     22     print("whee")
     23     print(DOUBLE(2))
     24     print(MAX(1, 2), __COUNTER__)
     25     if TESTING:
     26          print("whoo")
     27     print("hah")
     28 
     29 debug_func()
     30 #endif
     31 ```
     32 Running this yields 
     33 ```
     34 whee
     35 4
     36 2 0
     37 whoo
     38 hah
     39 ```
     40 
     41 `c-preprocessor` has support for the `#define`, `#ifdef`, `#endif`, `#ifndef`,
     42 `#include`, `#undef`, and `#else` directives as of now.
     43 
     44 Furthermore, the following special preprocessor macros are implemented: 
     45 - `__COUNTER__`: results in a higher number each time it is expanded
     46 - `__VERSION__`, `__PYTHON__`, `__PYTHON_MINOR__`, `__PYTHON_MICRO__`
     47 - `__IMPLEMENTATION__`: the Python implementation
     48 - `__FILE_NAME__`
     49 - `__BYTE_ORDER__`, `__ORDER_LITTLE_ENDIAN__`, `__ORDER_BIG_ENDIAN__`
     50 
     51 # install 
     52 There's not really a proper install process yet. You're gonna want to make a
     53 corresponding `.pth` file and install the package as normal.
     54 
     55 # how? 
     56 Python supports arbitrary file encodings which are implemented via Python
     57 code. This uses that interface to effectively reparse your code at runtime
     58 before it starts being evaluated. This is actually used by Dropbox to do inline
     59 HTML: see [here](https://github.com/dropbox/pyxl).