|
| 1 | +#! /usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# vim: set et sw=4 fenc=utf-8: |
| 4 | +# |
| 5 | +# func2str3.py |
| 6 | + |
| 7 | +""" |
| 8 | +This script demonstrates making the JsFunction class work both as a callable and as a decorator. Excluding **kwargs in the output. Added test flag. |
| 9 | +""" |
| 10 | + |
| 11 | +class JsFunction: |
| 12 | + def __init__(self, func, test=False): |
| 13 | + self.func = func |
| 14 | + self.test = test |
| 15 | + |
| 16 | + def __call__(self, *args, test=False, **kwargs): |
| 17 | + def wrapped_func(*args, **kwargs): |
| 18 | + args_str = ', '.join(repr(arg) for arg in args) |
| 19 | + #kwargs_str = ', '.join(f'{key}={repr(value)}' |
| 20 | + # for key, value in kwargs.items()) |
| 21 | + #all_args = ', '.join(filter(None, [args_str, kwargs_str])) |
| 22 | + |
| 23 | + return f"{self.func.__name__}({args_str})" |
| 24 | + |
| 25 | + data = ["init()"] |
| 26 | + if test or self.test: |
| 27 | + data.append("setRNG('testRandom')") |
| 28 | + |
| 29 | + data.append(wrapped_func(*args, **kwargs) if args else self.func(*args, **kwargs)) |
| 30 | + return data |
| 31 | + |
| 32 | + def __get__(self, instance, owner): |
| 33 | + return self if instance is None else types.MethodType(self, instance) |
| 34 | + |
| 35 | +# Applied as a decorator |
| 36 | +# Functional placeholder that uses the JS code, until the Python function is created |
| 37 | +@JsFunction |
| 38 | +def alpha(*args, **kwargs): |
| 39 | + pass |
| 40 | + |
| 41 | +# Eventually independent python function will be created |
| 42 | +def bravo(): |
| 43 | + return "bravo('aa', 'bb', 123)" |
| 44 | + |
| 45 | +# Create a alternately named instance of the JsFunction class that's callable |
| 46 | +# Which is important for testing against python functions |
| 47 | +testBravo = JsFunction(bravo, test=True) |
| 48 | +regBravo = JsFunction(bravo) |
| 49 | + |
| 50 | +""" |
| 51 | +WARNING - WARNING - WARNING - WARNING - WARNING - WARNING |
| 52 | +You cannot create an alternate instance using the JsFunction if it is already used. Attempting to do so may lead to unexpected behavior. For example, you cannot use testAlpha = JsFunction(alpha) if alpha() already has the JsFunction decorator applied. |
| 53 | +WARNING - WARNING - WARNING - WARNING - WARNING - WARNING |
| 54 | +""" |
| 55 | + |
| 56 | +print(alpha("aa", "bb", 123, x=123)) # Returns: alpha('aa', 'bb', 123, x=123) |
| 57 | +print(bravo()) # Returns: bravo('aa', 'bb', 123, x=123) |
| 58 | +print(testBravo("aa", "bb", 123, x=123)) # Returns: bravo('aa', 'bb', 123, x=123) |
| 59 | +print(regBravo("aa", "bb", 123, x=123)) # Returns: bravo('aa', 'bb', 123, x=123) |
| 60 | + |
| 61 | +def string_comparison(str1, str2): |
| 62 | + if str1 == str2: |
| 63 | + return "Test Passed: Strings are equal" |
| 64 | + else: |
| 65 | + return "Test Failed: Strings are not equal" |
| 66 | + |
| 67 | +print(string_comparison(alpha(123,'aaa'), testBravo(123,'aaa'))) |
| 68 | +print(string_comparison(bravo(), testBravo('aa', 'bb', 123, x=123))) |
0 commit comments