Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stas-prokopiev committed Jul 24, 2020
1 parent c2b0e9a commit 1017ed0
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 156 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# My ignore
jupyter_notebooks/*


# Temporary and binary files
*~
*.py[cod]
Expand Down
13 changes: 11 additions & 2 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
============
Contributors
Authors
============

* stanislav <stas.prokopiev@gmail.com>
* Stanislav Prokopyev <stas.prokopiev@gmail.com>

Contacts
========

* email: stas.prokopiev@gmail.com

* `vk.com <https://vk.com/stas.prokopyev>`_

* `Facebook <https://www.facebook.com/profile.php?id=100009380530321>`_
8 changes: 8 additions & 0 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Contributing
============

- Fork it (<https://github.com/stas-prokopiev/char/fork>)
- Create your feature branch (`git checkout -b feature/fooBar`)
- Commit your changes (`git commit -am 'Add some fooBar'`)
- Push to the branch (`git push origin feature/fooBar`)
- Create a new Pull Request
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
====
char
====
===
cat
===


Add a short description here!
Expand Down
17 changes: 0 additions & 17 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,17 +0,0 @@
# =============================================================================
# DEPRECATION WARNING:
#
# The file `requirements.txt` does not influence the package dependencies and
# will not be automatically created in the next version of PyScaffold (v4.x).
#
# Please have look at the docs for better alternatives
# (`Dependency Management` section).
# =============================================================================
#
# Add your pinned requirements so that they can be easily installed with:
# pip install -r requirements.txt
# Remember to also add them in setup.cfg but unpinned.
# Example:
# numpy==1.13.3
# scipy==1.0
#
25 changes: 17 additions & 8 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,40 @@

[metadata]
name = char
description = Add a short description here!
author = stanislav
description = decorator for fast check types of arguments which are given to function
author = Stanislav Prokopyev
author-email = stas.prokopiev@gmail.com
license = mit
long-description = file: README.rst
long-description-content-type = text/x-rst; charset=UTF-8
url = https://github.com/pyscaffold/pyscaffold/
project-urls =
Documentation = https://pyscaffold.org/
url = https://github.com/stas-prokopiev/char
# Change if running only on Windows, Mac or Linux (comma-separated)
platforms = any
# Add here all kinds of additional classifiers as defined under
# https://pypi.python.org/pypi?%3Aaction=list_classifiers
classifiers =
Development Status :: 4 - Beta
Development Status :: 3 - Alpha
Programming Language :: Python
Operating System :: OS Independent
License :: OSI Approved :: MIT License
Programming Language :: Python :: 2
Programming Language :: Python :: 2.7
Programming Language :: Python :: 3
Programming Language :: Python :: 3.4
Programming Language :: Python :: 3.5
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9

[options]
zip_safe = False
packages = find:
include_package_data = True
package_dir =
=src
# DON'T CHANGE THE FOLLOWING LINE! IT WILL BE UPDATED BY PYSCAFFOLD!
setup_requires = pyscaffold>=3.2a0,<3.3a0
# # DON'T CHANGE THE FOLLOWING LINE! IT WILL BE UPDATED BY PYSCAFFOLD!
# setup_requires = pyscaffold>=3.2a0,<3.3a0
# Add here dependencies of your project (semicolon/line-separated), e.g.
# install_requires = numpy; scipy
# The usage of test_requires is discouraged, see `Dependency Management` docs
Expand Down
28 changes: 17 additions & 11 deletions src/char/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# -*- coding: utf-8 -*-
from pkg_resources import get_distribution, DistributionNotFound

try:
# Change here if project is renamed and does not equal the package name
dist_name = __name__
__version__ = get_distribution(dist_name).version
except DistributionNotFound:
__version__ = 'unknown'
finally:
del get_distribution, DistributionNotFound
# Standard library imports
# Third party imports

# Local imports
from cat.main import cat

check_arguments_types = cat
__all__ = ["cat", "check_arguments_types"]


#####
# Prepare basic logger in case user is not setting it itself.
#####
import logging
LOGGER = logging.getLogger("check_arguments_types")
LOGGER.addHandler(logging.NullHandler())

108 changes: 108 additions & 0 deletions src/char/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# Standard library imports
import sys
import logging
# Third party imports

# Local imports


LOGGER = logging.getLogger("check_function_arguments")
LOGGER.addHandler(logging.NullHandler())


DICT_TUPLE_DEFAULT_TYPES_BY_PREFIX = {}
#####
# First define prefixes which are python version specific
if sys.version_info[0] == 2:
# basestring is parent class for str and unicode
DICT_TUPLE_DEFAULT_TYPES_BY_PREFIX["str_"] = (basestring)
else:
DICT_TUPLE_DEFAULT_TYPES_BY_PREFIX["str_"] = (str)
#####
DICT_TUPLE_DEFAULT_TYPES_BY_PREFIX["bytes_"] = (bytes)
DICT_TUPLE_DEFAULT_TYPES_BY_PREFIX["bool_"] = (bool)
DICT_TUPLE_DEFAULT_TYPES_BY_PREFIX["b_"] = (bool)
DICT_TUPLE_DEFAULT_TYPES_BY_PREFIX["is_"] = (bool)
DICT_TUPLE_DEFAULT_TYPES_BY_PREFIX["has_"] = (bool)
DICT_TUPLE_DEFAULT_TYPES_BY_PREFIX["int_"] = (int)
DICT_TUPLE_DEFAULT_TYPES_BY_PREFIX["i_"] = (int)
DICT_TUPLE_DEFAULT_TYPES_BY_PREFIX["float_"] = (float)
DICT_TUPLE_DEFAULT_TYPES_BY_PREFIX["f_"] = (float)
DICT_TUPLE_DEFAULT_TYPES_BY_PREFIX["list_"] = (list)
DICT_TUPLE_DEFAULT_TYPES_BY_PREFIX["l_"] = (list)
DICT_TUPLE_DEFAULT_TYPES_BY_PREFIX["dict_"] = (dict)
DICT_TUPLE_DEFAULT_TYPES_BY_PREFIX["d_"] = (dict)
DICT_TUPLE_DEFAULT_TYPES_BY_PREFIX["set_"] = (set)
DICT_TUPLE_DEFAULT_TYPES_BY_PREFIX["s_"] = (set)


def check_type_of_1_argument(
str_function_name,
str_argument_name,
argument_value,
tuple_types_var_can_be
):
""""""
if isinstance(argument_value, tuple_types_var_can_be):
return
str_error_message = (
"Incorrect type of variable was given to function: " +
str_function_name + "\n" +
"---> For variable: " + str(str_argument_name) + "\n" +
"---> Were given value: " + str(argument_value) + "\n" +
"---> With type: " + str(type(argument_value)) + "\n" +
"---> Instead of: " + str(tuple_types_var_can_be)
)
raise TypeError(str_error_message)


def cat(
function=None,
dict_tuple_types_by_prefix=None,
dict_tuple_types_by_prefix_to_update_default=None,
bool_is_to_skip_None_value=True,
):
""""""
if dict_tuple_types_by_prefix is None:
dict_tuple_types_by_prefix_local = DICT_TUPLE_DEFAULT_TYPES_BY_PREFIX
else:
dict_tuple_types_by_prefix_local = dict_tuple_types_by_prefix

def cfa_with_args(func_to_check):
""""""
int_args_count = func_to_check.__code__.co_argcount
list_function_arguments = \
func_to_check.__code__.co_varnames[:int_args_count]
str_function_name = func_to_check.__name__

def wrapper(*args, **kwargs):
dict_local_variables = locals()
tuple_args = dict_local_variables['args']
dict_kwargs = dict_local_variables['kwargs']
list_tuples_to_iterate = (
list(zip(list_function_arguments, tuple_args)) +
list(dict_kwargs.items())
)
#####
# Check arguments one by one
for str_argument_name, argument_value in list_tuples_to_iterate:
LOGGER.debug("Checking type of argument: %s", str_argument_name)
# By default if value is None then leave this argument alone
if argument_value is None and bool_is_to_skip_None_value:
continue
for str_prefix in dict_tuple_types_by_prefix_local:
if str_argument_name.startswith(str_prefix):
check_type_of_1_argument(
str_function_name,
str_argument_name,
argument_value,
dict_tuple_types_by_prefix_local[str_prefix]
)

#####
return func_to_check(*args, **kwargs)
return wrapper
if function:
return cfa_with_args(function)
return cfa_with_args

115 changes: 0 additions & 115 deletions src/char/skeleton.py

This file was deleted.

0 comments on commit 1017ed0

Please sign in to comment.