Advertisement
Python253

example_functions

Mar 12th, 2024 (edited)
600
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.53 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: example_functions.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6. # For use with Disassembler.py:  https://pastebin.com/Kzu3BZPM
  7.  
  8. """
  9. Example Python Script with Built-in Functions
  10.  
  11. This script serves as an example, defining various Python built-in functions alphabetically categorized.
  12. It is intended for use with the 'disassembler.py' script to explore the bytecode generated for each function and understand their internal workings.
  13.  
  14. Usage:
  15. 1. Utilize the functions defined in this script as a reference or for testing purposes.
  16. 2. Integrate these functions with the 'disassembler.py' script to investigate the bytecode produced for each function.
  17.  
  18. Note:
  19. The output of this script will show in the terminal only.
  20. Disassembled output will be saved to the current working directory when used with 'disassembler.py'.
  21. """
  22.  
  23. # Imports
  24. import sys
  25. import pydoc
  26. import io
  27.  
  28. # Class definition
  29. class ExampleClass:
  30.     attr = "example_attribute"
  31.    
  32. # Create Example Object
  33. example_object = ExampleClass()
  34.  
  35.  
  36. # List of Functions
  37.  
  38. # A
  39. def A_abs(x):
  40.     return abs(x)
  41.  
  42. def A_aiter(iterable):
  43.     it = iter(iterable)
  44.     return it
  45.  
  46. def A_all(iterable):
  47.     return all(iterable)
  48.  
  49. def A_anext(iterator, default=None):
  50.     try:
  51.         return next(iterator)
  52.     except StopIteration:
  53.         return default
  54.  
  55. def A_any(iterable):
  56.     return any(iterable)
  57.  
  58. def A_ascii(obj):
  59.     return ascii(obj)
  60.  
  61. # B
  62. def B_bin(x):
  63.     return bin(x)
  64.  
  65. def B_bool(x):
  66.     return bool(x)
  67.  
  68. def B_bytearray(source, encoding='utf-8', errors='strict'):
  69.     return bytearray(source, encoding, errors)
  70.  
  71. def B_bytes(x, encoding='utf-8', errors='strict'):
  72.     return bytes(x, encoding, errors)
  73.  
  74. # C
  75. def C_chr(i):
  76.     return chr(i)
  77.  
  78. def C_classmethod(func):
  79.     return classmethod(func)
  80.  
  81. def C_compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1):
  82.     code = compile(source, filename, mode, flags, dont_inherit, optimize)
  83.     return code
  84.  
  85. def C_complex(real=0, imag=0):
  86.     return complex(real, imag)
  87.  
  88. # D
  89. def D_dict(**kwargs):
  90.     return dict(**kwargs)
  91.  
  92. def D_dir(obj=None):
  93.     return dir(obj)
  94.  
  95. def D_divmod(a, b):
  96.     return divmod(a, b)
  97.  
  98. # E
  99. def E_enumerate(iterable, start=0):
  100.     return enumerate(iterable, start)
  101.  
  102. def E_eval(expression, globals=None, locals=None):
  103.     return eval(expression, globals, locals)
  104.  
  105. def E_exec_and_capture(source, globals=None, locals=None):
  106.     # Redirect stdout to capture the output
  107.     original_stdout = sys.stdout
  108.     sys.stdout = io.StringIO()
  109.  
  110.     try:
  111.         exec(source, globals, locals)
  112.         # Get the captured output
  113.         result = sys.stdout.getvalue()
  114.     finally:
  115.         # Restore the original stdout
  116.         sys.stdout = original_stdout
  117.  
  118.     return result
  119.  
  120.  
  121. # F
  122. def F_filter(function, iterable):
  123.     return filter(function, iterable)
  124.  
  125. def F_float(x):
  126.     return float(x)
  127.  
  128. def F_format(value, format_spec=''):
  129.     return format(value, format_spec)
  130.  
  131. def F_frozenset(iterable):
  132.     return frozenset(iterable)
  133.  
  134. # G
  135. def G_getattr(obj, name, default=None):
  136.     return getattr(obj, name, default)
  137.  
  138. def G_globals():
  139.     return globals()
  140.  
  141. # H
  142. def H_hasattr(obj, name):
  143.     return hasattr(obj, name)
  144.  
  145. def H_hash(obj):
  146.     return hash(obj)
  147.  
  148. def H_help(obj=None):
  149.     help(obj)
  150.  
  151. def H_hex(x):
  152.     return hex(x)
  153.  
  154. # I
  155. def I_id(obj):
  156.     return id(obj)
  157.  
  158. def I_input(prompt=''):
  159.     return input(prompt)
  160.  
  161. def I_int(x, base=10):
  162.     return int(x, base)
  163.  
  164. def I_isinstance(obj, classinfo):
  165.     return isinstance(obj, classinfo)
  166.  
  167. def I_issubclass(cls, classinfo):
  168.     return issubclass(cls, classinfo)
  169.  
  170. def I_iter(iterable):
  171.     return iter(iterable)
  172.  
  173. # L
  174. def L_len(obj):
  175.     return len(obj)
  176.  
  177. def L_list(iterable):
  178.     return list(iterable)
  179.  
  180. def L_locals():
  181.     return locals()
  182.  
  183. # M
  184. def M_map(func, *iterables):
  185.     return map(func, *iterables)
  186.  
  187. def M_max(iterable, *args, **kwargs):
  188.     return max(iterable, *args, **kwargs)
  189.  
  190. def M_memoryview(obj):
  191.     return memoryview(obj)
  192.  
  193. def M_min(iterable, *args, **kwargs):
  194.     return min(iterable, *args, **kwargs)
  195.  
  196. # N
  197. def N_next(iterator, default=None):
  198.     try:
  199.         return next(iterator)
  200.     except StopIteration:
  201.         return default
  202.  
  203. # O
  204. def O_object():
  205.     return object()
  206.  
  207. def O_oct(x):
  208.     return oct(x)
  209.  
  210. def O_ord(c):
  211.     return ord(c)
  212.  
  213. # P
  214. def P_pow(base, exp, mod=None):
  215.     return pow(base, exp, mod)
  216.  
  217. def P_print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False):
  218.     print(*objects, sep=sep, end=end, file=file, flush=flush)
  219.  
  220. def P_property(fget=None, fset=None, fdel=None, doc=None):
  221.     return property(fget, fset, fdel, doc)
  222.  
  223. # R
  224. def R_range(*args):
  225.     return range(*args)
  226.  
  227. def R_repr(obj):
  228.     return repr(obj)
  229.  
  230. def R_reversed(seq):
  231.     return reversed(seq)
  232.  
  233. def R_round(number, ndigits=None):
  234.     return round(number, ndigits)
  235.  
  236. # S
  237. def S_set(iterable):
  238.     return set(iterable)
  239.  
  240. def S_setattr(obj, name, value):
  241.     setattr(obj, name, value)
  242.  
  243. def S_slice(*args):
  244.     return slice(*args)
  245.  
  246. def S_sorted(iterable, key=None, reverse=False):
  247.     return sorted(iterable, key=key, reverse=reverse)
  248.  
  249. def S_staticmethod(func):
  250.     return staticmethod(func)
  251.  
  252. def S_sum(iterable, start=0):
  253.     return sum(iterable, start)
  254.  
  255. def S_super(cls, obj):
  256.     return super(cls, obj)
  257.  
  258. # T
  259. def T_tuple(iterable):
  260.     return tuple(iterable)
  261.  
  262. def T_type(object, bases, dict):
  263.     return type(object, bases, dict)
  264.  
  265. # V
  266. def V_vars(obj=None):
  267.     return vars(obj)
  268.  
  269. # Z
  270. def Z_zip(*iterables):
  271.     return zip(*iterables)
  272.  
  273. # _
  274. def underscore():
  275.     print("This function does something!")
  276.  
  277. # __import__
  278. def double_underscore_import(name, globals=None, locals=None, fromlist=(), level=0):
  279.     return __import__(name, globals, locals, fromlist, level)
  280.  
  281. # Example usage of all functions
  282. if __name__ == "__main__":
  283.     # A
  284.     result = A_abs(-42)
  285.     print("Absolute value:", result)
  286.  
  287.     iterator = A_aiter([1, 2, 3])
  288.     print("First element from iterator:", A_anext(iterator))
  289.  
  290.     values = [True, True, False]
  291.     print("All values are True:", A_all(values))
  292.  
  293.     print("Any value is True:", A_any(values))
  294.  
  295.     string = "Hello, Python!"
  296.     print("ASCII representation:", A_ascii(string))
  297.  
  298.     # B
  299.     binary = B_bin(42)
  300.     print("Binary representation:", binary)
  301.  
  302.     boolean = B_bool(42)
  303.     print("Boolean value:", boolean)
  304.  
  305.     byte_array = B_bytearray("hello", encoding='utf-8')
  306.     print("Bytearray:", byte_array)
  307.  
  308.     byte_string = B_bytes("hello", encoding='utf-8')
  309.     print("Bytes:", byte_string)
  310.  
  311.     # C
  312.     code_source = "print('Hello, from compiled code!')"
  313.     compiled_code = C_compile(code_source, '<string>', 'exec')
  314.     exec(compiled_code)
  315.  
  316.     complex_number = C_complex(2, 3)
  317.     print("Complex number:", complex_number)
  318.  
  319.     # D
  320.     example_dict = D_dict(a=1, b=2, c=3)
  321.     print("Dictionary:", example_dict)
  322.  
  323.     example_list = D_dir([1, 2, 3])
  324.     print("Directory of a list:", example_list)
  325.  
  326.     divmod_result = D_divmod(10, 3)
  327.     print("divmod result:", divmod_result)
  328.  
  329.     # E
  330.     enum_result = E_enumerate(['apple', 'banana', 'cherry'])
  331.     print("Enumerated result:", list(enum_result))
  332.  
  333.     eval_result = E_eval("5 * 5")
  334.     print("Eval result:", eval_result)
  335.  
  336.     executed_code_result = E_exec_and_capture("print('Hello, from executed code!')")
  337.     print("Executed code result:", executed_code_result)
  338.  
  339.     # F
  340.     filtered_result = F_filter(lambda x: x % 2 == 0, [1, 2, 3, 4, 5])
  341.     print("Filtered result:", list(filtered_result))
  342.  
  343.     float_result = F_float("3.14")
  344.     print("Float result:", float_result)
  345.  
  346.     format_result = F_format("Hello, {}!")
  347.     print("Formatted result:", format_result.format("World"))
  348.  
  349.     frozenset_result = F_frozenset([1, 2, 3])
  350.     print("Frozenset result:", frozenset_result)
  351.  
  352.     # G
  353.     getattr_result = G_getattr("example_string", "attr", "default_value")
  354.     print("Get attribute result:", getattr_result)
  355.  
  356.     globals_result = G_globals()
  357.     print("Globals result:", globals_result)
  358.  
  359.     # H
  360.     hasattr_result = H_hasattr("obj", "attr")
  361.     print("Has attribute result:", hasattr_result)
  362.  
  363.     hash_result = H_hash("hello")
  364.     print("Hash value:", hash_result)
  365.  
  366.     help_result = pydoc.render_doc(str)
  367.     print("Help output:", help_result)
  368.  
  369.     hex_result = H_hex(255)
  370.     print("Hexadecimal representation:", hex_result)
  371.  
  372.     # I
  373.     id_result = I_id("obj")
  374.     print("ID of object:", id_result)
  375.  
  376.     input_result = I_input("Enter something: ")
  377.     print("Input value:", input_result)
  378.  
  379.     int_result = I_int("42")
  380.     print("Integer value:", int_result)
  381.  
  382.     isinstance_result = I_isinstance(42, int)
  383.     print("Is instance:", isinstance_result)
  384.  
  385.     issubclass_result = I_issubclass(bool, int)
  386.     print("Is subclass:", issubclass_result)
  387.  
  388.     iterator_result = I_iter([1, 2, 3])
  389.     print("Iterator:", iterator_result)
  390.  
  391.     # L
  392.     len_result = L_len([1, 2, 3])
  393.     print("Length:", len_result)
  394.  
  395.     list_result = L_list((1, 2, 3))
  396.     print("List:", list_result)
  397.  
  398.     locals_result = L_locals()
  399.     print("Local variables:", locals_result)
  400.  
  401.     # M
  402.     map_result = M_map(lambda x: x * 2, [1, 2, 3])
  403.     print("Mapped result:", list(map_result))
  404.  
  405.     max_result = M_max([1, 2, 3])
  406.     print("Maximum value:", max_result)
  407.  
  408.     memoryview_result = M_memoryview(b"Hello")
  409.     print("Memoryview:", memoryview_result)
  410.  
  411.     min_result = M_min([1, 2, 3])
  412.     print("Minimum value:", min_result)
  413.  
  414.     # N
  415.     next_result = N_next(iter([1, 2, 3]))
  416.     print("Next element:", next_result)
  417.  
  418.     # O
  419.     object_result = O_object()
  420.     print("Object:", object_result)
  421.  
  422.     oct_result = O_oct(8)
  423.     print("Octal representation:", oct_result)
  424.  
  425.     ord_result = O_ord("A")
  426.     print("Ordinal value:", ord_result)
  427.  
  428.     # P
  429.     pow_result = P_pow(2, 3)
  430.     print("Power result:", pow_result)
  431.  
  432.     P_print("Hello, Python!")
  433.  
  434.     property_result = P_property()
  435.     print("Property:", property_result)
  436.  
  437.     # R
  438.     range_result = R_range(5)
  439.     print("Range:", list(range_result))
  440.  
  441.     repr_result = R_repr("Hello")
  442.     print("Representation:", repr_result)
  443.  
  444.     reversed_result = R_reversed([1, 2, 3])
  445.     print("Reversed:", list(reversed_result))
  446.  
  447.     round_result = R_round(3.14159, 2)
  448.     print("Rounded value:", round_result)
  449.  
  450.     # S
  451.     set_result = set([1, 2, 3])
  452.     print("Set:", set_result)
  453.  
  454.     example_object = ExampleClass()
  455.     setattr(example_object, "attr", 42)
  456.     print("Set attribute result:", example_object.attr)
  457.  
  458.     slice_result = slice(1, 5, 2)
  459.     print("Slice:", slice_result)
  460.  
  461.     sorted_result = sorted([3, 1, 2])
  462.     print("Sorted list:", sorted_result)
  463.  
  464.     staticmethod_result = staticmethod(lambda x: x)
  465.     print("Static method:", staticmethod_result)
  466.  
  467.     sum_result = sum([1, 2, 3])
  468.     print("Sum:", sum_result)
  469.  
  470.     super_result = super(str, "obj")
  471.     print("Super:", super_result)
  472.  
  473.    
  474.     # T
  475.     tuple_result = T_tuple([1, 2, 3])
  476.     print("Tuple:", tuple_result)
  477.  
  478.     type_result = T_type("obj", (object,), {})
  479.     print("Type:", type_result)
  480.  
  481.     # V
  482.     vars_result = V_vars(example_object)
  483.     print("Variables dictionary:", vars_result)
  484.  
  485.     # Z
  486.     zip_result = Z_zip([1, 2, 3], ["a", "b", "c"])
  487.     print("Zipped result:", list(zip_result))
  488.  
  489.     # _
  490.     underscore()
  491.  
  492.     # __import__
  493.     import_result = double_underscore_import("math")
  494.     print("Imported module:", import_result)
  495.    
  496.     # Message
  497.     print("All Function Testing Has Completed!\n\nEnding Program...\n")
  498.    
  499.  
  500.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement