Giter Club home page Giter Club logo

privates.py's Introduction

privates.py

PyPI - Version Tests Build


Installation

Linux/macOS

python3 -m pip install -U privates.py

Windows

py -3 -m pip install -U privates.py

Example

from privates import private

@private
class Hello:
    __readonly__ = "bar",

    def __init__(self):
        self.bar = "hello world!"

hello = Hello()
print(hello.bar)  # hello world!
hello.bar = "goodbye, world :("  # AccessError

License

privates.py is distributed under the terms of the MIT license.

privates.py's People

Contributors

zerointensity avatar

Stargazers

Hasan Sezer Taşan avatar Gaël Écorchard avatar cemrehancavdar avatar wassaf shahzad avatar

Watchers

 avatar  avatar

privates.py's Issues

Preventing class and static attribute access on private types

As of now, @private and @class_modifier allow users outside of the scope to access class variables, class methods, and static methods. This is because we can't hook on to type, without some black magic at least. So, let's add the black magic!

We can only have one function wrapped to type.getattribute, so we need some sort of global storage for all types decorated with supports_private. Then, we can use ctypes (or alternatively, a C extension, but then we have to bring cibuildwheel into the build step) to modify the object structure and set tp_getattr. Finally, we need to call PyType_Ready (or PyType_Modified, I can't remember).

Automatic Injection

Putting @supports_private on every class might get tedious for some users. privates.py could add an inject call or something similar that goes through the global scope (via get_back_frame().f_globals) and calls supports_privates on each type. This shouldn't apply to built-in types though (such as str). An example would look like:

from privates import inject

inject()

# No @supports_privates is needed!
class Foo:
    __readonly__ = "bar",

    def __init__(self) -> None:
        self.bar = "hello world"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.