Skip to content

Convert properties in Cython extension classes from the deprecated legacy syntax to the decorator syntax.

License

Notifications You must be signed in to change notification settings

JoeSchiff/Cython_property_converter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cython Property Converter

Convert properties in Cython extension classes from the deprecated legacy syntax to the decorator syntax.



Is this what you're trying to do?

https://cython.readthedocs.io/en/stable/src/userguide/extension_types.html#properties



Table of Contents



Summary

Convert from this:

    property cheese:
        "A doc string can go here."
        def __get__(self):
            ...
        def __set__(self, value):
            ...
        def __del__(self):
            ...

to this:

    @property
    def cheese(self):
        "A doc string can go here."
        ...
    @cheese.setter
    def cheese(self, value):
        ...
    @cheese.deleter
    def cheese(self):
        ...



Basic Usage

Give it a folder with the files to convert and a folder to put the new files:
python converter.py -i /path/to/files/ -o /path/to/output/



Optional Arguments

  -h, --help            show this help message and exit
  --input_dir [INPUT_DIR], -i [INPUT_DIR]
                        Path of the folder containing the files to be modified. Default: Current working directory.
  --output_dir [OUTPUT_DIR], -o [OUTPUT_DIR]
                        Path of the folder to save the modified files. Default: create a subfolder called `new_syntax` where the script is located.
  --class_declaration {cython,pure_python}, -c {cython,pure_python}
                        Which class declaration syntax to use. cython: `cdef class Spam:` or pure_python: `@cython.cclass class Spam:`. Default: cython
  --output_mod_only OUTPUT_MOD_ONLY, -m OUTPUT_MOD_ONLY
                        True/False. Output all files or only modified files. Default: False (output all files)
  --no_getter {skip,convert}, -n {skip,convert}
                        The new syntax must have a getter method before using a setter or deleter method. If a getter method does not exist for that property, you must either keep the old
                        syntax (`skip`) or create an empty getter method (`convert`). Default: skip



Class declaration syntax

Choose either Pure Python:
python converter.py --class_declaration pure_python

@cython.cclass
class Spam:
    @property
    def cheese(self):
        ...

or Cython:
python converter.py --class_declaration cython

cdef class Spam:
    @property
    def cheese(self):
        ...



Missing getter method

Consider this example of a valid setter method without a getter method using the old syntax:

cdef class spam:
    property url_match:
        def __set__(self, url_match):
            self._url_match = url_match
            self._reload_special_cases()

However, the new syntax does not allow using a setter or deleter method without a getter method. Use the --no_getter argument to decide how to handle this.

You can either skip this property and keep the old syntax unchanged:
python converter.py --no_getter skip

or create an empty getter method and continue with the conversion:
python converter.py --no_getter convert

cdef class spam:
    @property
    def url_match(self):
        pass
    @url_match.setter
    def url_match(self, url_match):
        self._url_match = url_match
        self._reload_special_cases()



Doc strings

Since there cannot be a docstring between the decorator and the def statement, the docstrings will be moved from here:

    property cheese:
        "A doc string can go here."
        def __get__(self):
            ...

to here:

    @property
    def cheese(self):
        "A doc string can go here."
        ...



Aditionally, one-line statements with a doctstring will be split from this:

    property split:
        "docstring for split"
        def __get__(self): return self.val

to this:

    @property
    def split(self):
        "docstring for split"
        return self.val



Requirements

Compatible with indents that use spaces or tabs. However, an indent must not contain both tabs and spaces.

Required Python versions >= 3.8
Required packages: cython



About

Convert properties in Cython extension classes from the deprecated legacy syntax to the decorator syntax.

Resources

License

Stars

Watchers

Forks

Packages

No packages published