gpt4 book ai didi

python - 有什么方法可以像Python中的C++宏函数那样替换数字吗?

转载 作者:太空宇宙 更新时间:2023-11-03 14:14:51 24 4
gpt4 key购买 nike

当我开发 C++ 应用程序时,我更喜欢使用宏函数来表示常量。它无疑增强了代码的可读性。

我搜索过在python上应用的方法,但没有找到。

如果Python语法上存在类似的函数,请告诉我如何做,如果不存在,请告诉我为什么它不存在,并结合Python的特性。

谢谢。

最佳答案

您希望非程序员能够更改这些值,所以这是一种配置?

您可以创建一个仅分配全局变量的单独文件,然后导入它,或者读取并执行它。

示例(如 https://github.com/QQuick/Opy 中使用的):

#====================================================================================================
# General options
# All options have default value False
#====================================================================================================

obfuscate_strings = True # Don't rely on this for sensitive information
obfuscated_name_tail = '_opy_' # Will be added to all obfuscated names to avoid clashes with plain names
plain_marker = '_opy_' # Strings or comments containing this marker will not be obfuscated
pep8_comments = True # If True, only inline comments of the form <blank><blank>#<blank>
# will be recognized, allowing # to appear in strings as long as
# it doesn't have that particular form



#====================================================================================================
# Extensions of files that should be obfuscated
# Typically these are files containing Python source code
# Other files are just copied to the target directory
#====================================================================================================

source_extensions = '''
py
pyx
'''



#====================================================================================================
# Extensions of files that should neither be obfuscated nor copied to the target directory
# Typically these are .pyc files, which could easily be decompiled, breaking obfuscation
#====================================================================================================

skip_extensions = '''
pyc
'''



#====================================================================================================
# Fragments that, when occuring in the path of a file, will cause this file to be ignored
# In other words, such files will neither be ofuscated nor copied
# Use this to exclude complete directories from any processing by Opy
# N.B. Use forward slashes rather than backslashes, also on Windows!
#====================================================================================================

skip_path_fragments = '''
test_dummy
'''



#====================================================================================================
# Modules in sys.path containing identifiers that should not be obfuscated
# Typically these are installed standard or 3rd party modules of which you have no source code
# Use dotted names if needed, e.g. include both matplotlib and matplotlib.pyplot
#====================================================================================================

external_modules = '''
re
os
sys
errno
keyword
importlib
random
codecs
shutil
'''



#====================================================================================================
# Relative path + name of Python source files containing identifiers that should not be obfuscated
# Typically these are human readable settings files loaded and exec'ed at runtime by your program
# Do not use this facility for files that are imported, that's what external_modules is for
# Also don't use it to keep the original file name for your main module, that what plain_names is for
#====================================================================================================

plain_files = '''
opy_config.txt
'''



#====================================================================================================
# Extra identifiers and module names (as opposed to contents) that should not be obfuscated
# Probably at least the names of your main modules (so not their filenames) go here
# If e.g. your main module is in my_main_module.py, include my_main_module in plain_names
#====================================================================================================

plain_names = '''
opy
poly_walker_test
'''

通过以下代码读取:

# =========== Read config file

try:
configFile = open (configFilePath)
except Exception as exception:
print (exception)
printHelpAndExit (1)

exec (configFile.read ())
configFile.close ()

def getConfig (parameter, default):
try:
return eval (parameter)
except:
return default

obfuscateStrings = getConfig ('obfuscate_strings', False)
asciiStrings = getConfig ('ascii_strings', False)
obfuscatedNameTail = getConfig ('obfuscated_name_tail', '_{}_'.format (programName))
plainMarker = getConfig ('plain_marker', '_{}_'.format (programName))
pep8Comments = getConfig ('pep8_comments', True)
sourceFileNameExtensionList = getConfig ('source_extensions.split ()', ['py', 'pyx'])
skipFileNameExtensionList = getConfig ('skip_extensions.split ()', ['pyc'])
skipPathFragmentList = getConfig ('skip_path_fragments.split ()', [])
externalModuleNameList = getConfig ('external_modules.split ()', [])
plainFileRelPathList = getConfig ('plain_files.split ()', [])
extraPlainWordList = getConfig ('plain_names.split ()', [])

关于python - 有什么方法可以像Python中的C++宏函数那样替换数字吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48257959/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com