Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: example_functions.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- # For use with Disassembler.py: https://pastebin.com/Kzu3BZPM
- """
- Example Python Script with Built-in Functions
- This script serves as an example, defining various Python built-in functions alphabetically categorized.
- It is intended for use with the 'disassembler.py' script to explore the bytecode generated for each function and understand their internal workings.
- Usage:
- 1. Utilize the functions defined in this script as a reference or for testing purposes.
- 2. Integrate these functions with the 'disassembler.py' script to investigate the bytecode produced for each function.
- Note:
- The output of this script will show in the terminal only.
- Disassembled output will be saved to the current working directory when used with 'disassembler.py'.
- """
- # Imports
- import sys
- import pydoc
- import io
- # Class definition
- class ExampleClass:
- attr = "example_attribute"
- # Create Example Object
- example_object = ExampleClass()
- # List of Functions
- # A
- def A_abs(x):
- return abs(x)
- def A_aiter(iterable):
- it = iter(iterable)
- return it
- def A_all(iterable):
- return all(iterable)
- def A_anext(iterator, default=None):
- try:
- return next(iterator)
- except StopIteration:
- return default
- def A_any(iterable):
- return any(iterable)
- def A_ascii(obj):
- return ascii(obj)
- # B
- def B_bin(x):
- return bin(x)
- def B_bool(x):
- return bool(x)
- def B_bytearray(source, encoding='utf-8', errors='strict'):
- return bytearray(source, encoding, errors)
- def B_bytes(x, encoding='utf-8', errors='strict'):
- return bytes(x, encoding, errors)
- # C
- def C_chr(i):
- return chr(i)
- def C_classmethod(func):
- return classmethod(func)
- def C_compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1):
- code = compile(source, filename, mode, flags, dont_inherit, optimize)
- return code
- def C_complex(real=0, imag=0):
- return complex(real, imag)
- # D
- def D_dict(**kwargs):
- return dict(**kwargs)
- def D_dir(obj=None):
- return dir(obj)
- def D_divmod(a, b):
- return divmod(a, b)
- # E
- def E_enumerate(iterable, start=0):
- return enumerate(iterable, start)
- def E_eval(expression, globals=None, locals=None):
- return eval(expression, globals, locals)
- def E_exec_and_capture(source, globals=None, locals=None):
- # Redirect stdout to capture the output
- original_stdout = sys.stdout
- sys.stdout = io.StringIO()
- try:
- exec(source, globals, locals)
- # Get the captured output
- result = sys.stdout.getvalue()
- finally:
- # Restore the original stdout
- sys.stdout = original_stdout
- return result
- # F
- def F_filter(function, iterable):
- return filter(function, iterable)
- def F_float(x):
- return float(x)
- def F_format(value, format_spec=''):
- return format(value, format_spec)
- def F_frozenset(iterable):
- return frozenset(iterable)
- # G
- def G_getattr(obj, name, default=None):
- return getattr(obj, name, default)
- def G_globals():
- return globals()
- # H
- def H_hasattr(obj, name):
- return hasattr(obj, name)
- def H_hash(obj):
- return hash(obj)
- def H_help(obj=None):
- help(obj)
- def H_hex(x):
- return hex(x)
- # I
- def I_id(obj):
- return id(obj)
- def I_input(prompt=''):
- return input(prompt)
- def I_int(x, base=10):
- return int(x, base)
- def I_isinstance(obj, classinfo):
- return isinstance(obj, classinfo)
- def I_issubclass(cls, classinfo):
- return issubclass(cls, classinfo)
- def I_iter(iterable):
- return iter(iterable)
- # L
- def L_len(obj):
- return len(obj)
- def L_list(iterable):
- return list(iterable)
- def L_locals():
- return locals()
- # M
- def M_map(func, *iterables):
- return map(func, *iterables)
- def M_max(iterable, *args, **kwargs):
- return max(iterable, *args, **kwargs)
- def M_memoryview(obj):
- return memoryview(obj)
- def M_min(iterable, *args, **kwargs):
- return min(iterable, *args, **kwargs)
- # N
- def N_next(iterator, default=None):
- try:
- return next(iterator)
- except StopIteration:
- return default
- # O
- def O_object():
- return object()
- def O_oct(x):
- return oct(x)
- def O_ord(c):
- return ord(c)
- # P
- def P_pow(base, exp, mod=None):
- return pow(base, exp, mod)
- def P_print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False):
- print(*objects, sep=sep, end=end, file=file, flush=flush)
- def P_property(fget=None, fset=None, fdel=None, doc=None):
- return property(fget, fset, fdel, doc)
- # R
- def R_range(*args):
- return range(*args)
- def R_repr(obj):
- return repr(obj)
- def R_reversed(seq):
- return reversed(seq)
- def R_round(number, ndigits=None):
- return round(number, ndigits)
- # S
- def S_set(iterable):
- return set(iterable)
- def S_setattr(obj, name, value):
- setattr(obj, name, value)
- def S_slice(*args):
- return slice(*args)
- def S_sorted(iterable, key=None, reverse=False):
- return sorted(iterable, key=key, reverse=reverse)
- def S_staticmethod(func):
- return staticmethod(func)
- def S_sum(iterable, start=0):
- return sum(iterable, start)
- def S_super(cls, obj):
- return super(cls, obj)
- # T
- def T_tuple(iterable):
- return tuple(iterable)
- def T_type(object, bases, dict):
- return type(object, bases, dict)
- # V
- def V_vars(obj=None):
- return vars(obj)
- # Z
- def Z_zip(*iterables):
- return zip(*iterables)
- # _
- def underscore():
- print("This function does something!")
- # __import__
- def double_underscore_import(name, globals=None, locals=None, fromlist=(), level=0):
- return __import__(name, globals, locals, fromlist, level)
- # Example usage of all functions
- if __name__ == "__main__":
- # A
- result = A_abs(-42)
- print("Absolute value:", result)
- iterator = A_aiter([1, 2, 3])
- print("First element from iterator:", A_anext(iterator))
- values = [True, True, False]
- print("All values are True:", A_all(values))
- print("Any value is True:", A_any(values))
- string = "Hello, Python!"
- print("ASCII representation:", A_ascii(string))
- # B
- binary = B_bin(42)
- print("Binary representation:", binary)
- boolean = B_bool(42)
- print("Boolean value:", boolean)
- byte_array = B_bytearray("hello", encoding='utf-8')
- print("Bytearray:", byte_array)
- byte_string = B_bytes("hello", encoding='utf-8')
- print("Bytes:", byte_string)
- # C
- code_source = "print('Hello, from compiled code!')"
- compiled_code = C_compile(code_source, '<string>', 'exec')
- exec(compiled_code)
- complex_number = C_complex(2, 3)
- print("Complex number:", complex_number)
- # D
- example_dict = D_dict(a=1, b=2, c=3)
- print("Dictionary:", example_dict)
- example_list = D_dir([1, 2, 3])
- print("Directory of a list:", example_list)
- divmod_result = D_divmod(10, 3)
- print("divmod result:", divmod_result)
- # E
- enum_result = E_enumerate(['apple', 'banana', 'cherry'])
- print("Enumerated result:", list(enum_result))
- eval_result = E_eval("5 * 5")
- print("Eval result:", eval_result)
- executed_code_result = E_exec_and_capture("print('Hello, from executed code!')")
- print("Executed code result:", executed_code_result)
- # F
- filtered_result = F_filter(lambda x: x % 2 == 0, [1, 2, 3, 4, 5])
- print("Filtered result:", list(filtered_result))
- float_result = F_float("3.14")
- print("Float result:", float_result)
- format_result = F_format("Hello, {}!")
- print("Formatted result:", format_result.format("World"))
- frozenset_result = F_frozenset([1, 2, 3])
- print("Frozenset result:", frozenset_result)
- # G
- getattr_result = G_getattr("example_string", "attr", "default_value")
- print("Get attribute result:", getattr_result)
- globals_result = G_globals()
- print("Globals result:", globals_result)
- # H
- hasattr_result = H_hasattr("obj", "attr")
- print("Has attribute result:", hasattr_result)
- hash_result = H_hash("hello")
- print("Hash value:", hash_result)
- help_result = pydoc.render_doc(str)
- print("Help output:", help_result)
- hex_result = H_hex(255)
- print("Hexadecimal representation:", hex_result)
- # I
- id_result = I_id("obj")
- print("ID of object:", id_result)
- input_result = I_input("Enter something: ")
- print("Input value:", input_result)
- int_result = I_int("42")
- print("Integer value:", int_result)
- isinstance_result = I_isinstance(42, int)
- print("Is instance:", isinstance_result)
- issubclass_result = I_issubclass(bool, int)
- print("Is subclass:", issubclass_result)
- iterator_result = I_iter([1, 2, 3])
- print("Iterator:", iterator_result)
- # L
- len_result = L_len([1, 2, 3])
- print("Length:", len_result)
- list_result = L_list((1, 2, 3))
- print("List:", list_result)
- locals_result = L_locals()
- print("Local variables:", locals_result)
- # M
- map_result = M_map(lambda x: x * 2, [1, 2, 3])
- print("Mapped result:", list(map_result))
- max_result = M_max([1, 2, 3])
- print("Maximum value:", max_result)
- memoryview_result = M_memoryview(b"Hello")
- print("Memoryview:", memoryview_result)
- min_result = M_min([1, 2, 3])
- print("Minimum value:", min_result)
- # N
- next_result = N_next(iter([1, 2, 3]))
- print("Next element:", next_result)
- # O
- object_result = O_object()
- print("Object:", object_result)
- oct_result = O_oct(8)
- print("Octal representation:", oct_result)
- ord_result = O_ord("A")
- print("Ordinal value:", ord_result)
- # P
- pow_result = P_pow(2, 3)
- print("Power result:", pow_result)
- P_print("Hello, Python!")
- property_result = P_property()
- print("Property:", property_result)
- # R
- range_result = R_range(5)
- print("Range:", list(range_result))
- repr_result = R_repr("Hello")
- print("Representation:", repr_result)
- reversed_result = R_reversed([1, 2, 3])
- print("Reversed:", list(reversed_result))
- round_result = R_round(3.14159, 2)
- print("Rounded value:", round_result)
- # S
- set_result = set([1, 2, 3])
- print("Set:", set_result)
- example_object = ExampleClass()
- setattr(example_object, "attr", 42)
- print("Set attribute result:", example_object.attr)
- slice_result = slice(1, 5, 2)
- print("Slice:", slice_result)
- sorted_result = sorted([3, 1, 2])
- print("Sorted list:", sorted_result)
- staticmethod_result = staticmethod(lambda x: x)
- print("Static method:", staticmethod_result)
- sum_result = sum([1, 2, 3])
- print("Sum:", sum_result)
- super_result = super(str, "obj")
- print("Super:", super_result)
- # T
- tuple_result = T_tuple([1, 2, 3])
- print("Tuple:", tuple_result)
- type_result = T_type("obj", (object,), {})
- print("Type:", type_result)
- # V
- vars_result = V_vars(example_object)
- print("Variables dictionary:", vars_result)
- # Z
- zip_result = Z_zip([1, 2, 3], ["a", "b", "c"])
- print("Zipped result:", list(zip_result))
- # _
- underscore()
- # __import__
- import_result = double_underscore_import("math")
- print("Imported module:", import_result)
- # Message
- print("All Function Testing Has Completed!\n\nEnding Program...\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement