Skip to main content

A python library for function decorators to restrict access to functions and much more

Project description

Implementation Test CD

Overview

You must have often wondered about how to make a function restricted to the user so that it could only be called a specific number of time, or even a function that is password protected.

This library provides an array of function decorators that can be used for the same.

Table of Contents

Features

  • Easily restrict any function according to your needs.
  • improve overall acessibility of your program.

Installation

Install using pip.

pip install deny-me

Usage

  • Once/Twice

To make your function run only once.

import the decorators.

from deny_me.restrict import once, twice

use acording to your needs.

# demo function
# This function will be callable only once.
# Repeated calling will generate error.
class some_class:
    def __init__(self):
        ...

    @once
    def function1(self, name: str, school: str = "ABC"):
        print(f"{name} goes to {school}!")
        ...

    # demo funtion 2
    # This function will be callable two times.
    # will generate error if called more than twice
    @twice
    def function2(self, *args, **kwargs):
        ...

Note to the users.

If other decorators are used for a function other than these, then it is recommended to use these decorators at the last.

For example:

# demo class
class Demo:
    def __init__(self):
        ...
    
    # suppose you need to define a property
    # that is only callable once.
    @property
    # use property first
    @once
    # and then use once
    def property1(self):
        return ...
    
    # similarly if you want to define a setter
    # for the property.
    @property1.setter # use this
    # and then the once decorator
    @once
    def property1(self, value: str):
        self.value = value
        ...
  • Run custom times

To run a function n times.

import the Allow class.

from deny_me.restrict import Allow

run n=10 times

# demo function
# this function will run 10 times

class Demo:
    def __init__(Self):
        ...
    
    @Allow(10).times
    def function(self, *args, **kwargs):
        ...

Note: Similar to the Once/Twice decorators, If other decorators are needed, such as property, then it should be at the top and the Allow(n).times should be the last decorator.

  • Password Protection

To make your function password protected.

import the Password class.

from deny_me.password import Password

Use the password decorator.

# demo function
# this function will be password protected.
class some_class:
    def __init__(self):
        ...

    @Password("pass123").protected
    def function(*args, **kwargs):
        ...

    # It is recommended that the above function has `*args` 
    # and `**kwargs` in its parameters.
    # If some parameters are mandatory, they can be defined before 
    # mentioning `*args` and `**kwargs`.
    ## FOR EXAMPLE:

    @Password("pass123").protected
    def function(name: str, school: str = "ABC", *args, **kwargs):
        ... 

How to call the function?

# To call the function, just add `password=` parameter 
# while calling.

c = some_class()
c.function(name="hehe", password="pass123")

# This will run fine as the password is correct!
# However, if the password is wrong, it will
# prompt about the same.

# NOTE: if the `password=` parameter is missing while calling
# the function then it will raise an error stating that
# an exclusive feature has been called that is not made
# available to the users.

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

deny_me-1.1.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

deny_me-1.1-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file deny_me-1.1.tar.gz.

File metadata

  • Download URL: deny_me-1.1.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for deny_me-1.1.tar.gz
Algorithm Hash digest
SHA256 fd7da4b35cdc6e039fb3ecfc68c7ac5b279dc8e061f747a03f086bc8ff42eb70
MD5 38d333bae4f1b2df5f4637732f72a684
BLAKE2b-256 b26162f3d33278cc0d37f78efc548aaa721a610c70fa13e68f24ebcffe7b95ea

See more details on using hashes here.

File details

Details for the file deny_me-1.1-py3-none-any.whl.

File metadata

  • Download URL: deny_me-1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for deny_me-1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0e7006aefab954876c5295cdb3ba6f46de3fd2ef17308cd4ff01c10b3a0329dd
MD5 9200d89c732a6987ee96785a2215fd27
BLAKE2b-256 b5bc1263fe557fbbad0e0d44ff49699532fcc94ee8a6b66e8d362d3cad8ba73f

See more details on using hashes here.

Supported by

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