一把用於自定義數字系統數量的瑞士軍刀。
Evgueni Antonov 2023。
該模塊是在Python 3.10.6,PIP 23.1.2上創建和測試的。
第一次出版於2023年5月。
pip3 install custom-numbers
# or
python3 -m pip install custom-numbers
有關如何使用它的詳細信息,請參見下面的“使用”部分。
from custom_numbers import custom_numbers as cn
my_custom_numeral_system = cn.CustomNumeralSystem("paf")
my_number = cn.CustomNumber(my_custom_numeral_system, "a")
# Now if you type in REPL:
# help(cn)
# You will get the help for the module and the classes.
該軟件包包含一個模塊,其中很少有類,可以幫助您聲明由具有自定義基礎的自定義符號製成的自定義數字系統。這聽起來很奇怪,實際上這就是它的外觀:
注意:不支持子系統,我不打算實施此功能。
注意:此模塊僅支持自定義整數號碼。自定義的浮點數不支持,也永遠不會。
讓我們從您已經知道的東西開始 - 二進制號碼。這些僅由零和一個製成,因此基部為2。
示例:1100101
這實際上是90年代真正酷的歌曲的標題。同樣是1100101二進制,轉換為小數為101 (一百個),就像(二進制) 10 2 == 2 10 (十進制)一樣。
現在 - 十六進制的數字 - 這些由數字0、1、2、3、4、5、6、7、8、9和字母A,B,C,D,E,F的數字製成,其基數為16。
因此,十六進制數字看起來像這樣:
F 16 == 15 10,10 16 == 16 10 ,F0 16 == 240 10
另一個眾所周知的數字系統是八進制系統,僅由零到7和第8位數字組成,但我們現在會跳過這個。
最後,我們從小就知道的十進制系統的基礎為10,並且由數字從零到9的數字製成。
好的,到目前為止,您已經熟悉最廣泛使用的數字系統 - 二進制,八進制,十進制和十六進制。
讓我們從重新定義二進制系統開始。因此,它確實有2個基礎,我們不會改變這一點。但是,如何使用0和1使用p和a ?因此, P是我們的新零, A將是我們的新零。因此,正如我們通常會寫1100101的那樣,現在將寫為Aappapa 。令人困惑,對吧?但是,什麼 - 它仍然是二進制系統,我們只是更改了符號。
但是現在,瘋狂的東西怎麼樣 - 使用符號p,a,f作為數字的數字系統,f, f將為2個小數, p為0小數。所以:
AA 3 == 4 10 ,AF 3 == 5 10 ,FP 3 == 6 10
現在讓我們看看這一點:
from custom_numbers import custom_numbers as cn
sys3 = cn.CustomNumeralSystem("paf")
num3 = cn.CustomNumber(sys3, "aa")
num10 = num3.to_decimal()
print(num10) # Prints "4"
測試這些類是否按預期工作的最佳方法是用我們都知道的符號聲明數字系統:
from custom_numbers import custom_numbers as cn
sys2 = cn.CustomNumeralSystem("01")
num2 = cn.CustomNumber(sys2, "1100101")
num10 = num2.to_decimal()
print(num10) # Prints "101"
sys16 = cn.CustomNumeralSystem("0123456789abcdef")
num16 = cn.CustomNumber(sys16, "f0")
num10 = num16.to_decimal()
print(num10) # Prints "240"
到目前為止,一切都很好。現在,讓我們完全發瘋,並聲明一個基本的Greather系統,大於16,並且是數字的完全怪異的符號。我們實際上可以做到這一點,甚至可以完全瘋狂的事情:
sysN = cn.CustomNumeralSystem("kje5nCs21Q9vW0KMqc")
注意:自定義數字很敏感!所以n! = n !
注意:有一些禁止字符,這些字符不能在數字系統中使用,也不能在數字中使用。您總是可以使用CustomNumeralSystem類的Forbidden_Characters屬性來將它們作為字符串作為字符串。
在用於基本驗證需求的CustomNumeralSystem類中,實現了平等和IQEELATY PYTHON運算符,因此您可以比較兩個對象。
但是,比較將按代表數字的字符的列表(基本上是字符串),而不是標準的python對象(參考)比較。
sys1 = cn.CustomNumeralSystem("paf")
sys2 = cn.CustomNumeralSystem("paf")
# The two objects are different, despite being initialized with
# the same value
id(sys1) == id(sys2) # False
# However the set of characters (the digits) is the same, the
# base is the same, so I accept they are the same numeral systems
sys1 == sys2 # True
# And you could also test for inequality
sys1 = cn.CustomNumeralSystem("paf")
sys2 = cn.CustomNumeralSystem("paz")
sys1 != sys2 # True
也支持簽名的自定義號碼。
sysN = cn.CustomNumeralSystem("paf")
numN1 = cn.CustomNumber(sysN, "-a") # A negative number
numN2 = cn.CustomNumber(sysN, "a") # A positive number
numN3 = cn.CustomNumber(sysN, "+a") # A positive number
基本數學操作是支持的槽標準Python操作員。
sysN = cn.CustomNumeralSystem("paf")
numN1 = cn.CustomNumber(sysN, "-a")
numN2 = cn.CustomNumber(sysN, "a")
numN3 = cn.CustomNumber(sysN, "+a")
# Comparisson
numN1 == numN2
numN1 != numN2
numN1 > numN2
numN1 < numN2
numN1 >= numN2
numN1 <= numN2
# Basic mathematical operations
numN1 + numN2 # Addition
numN1 += numN2 # Augmented addition
numN1 - numN2 # Subtraction
numN1 -= numN2 # Augmented subtraction
numN1 // numN2 # Floor division
numN1 / numN2 # NOTE: This will perform floor division as well!
# as floating point numbers are not supported by this class and will
# never be.
numN1 * numN2 # Multiplication
numN1 ** numN2 # Power
numN1 % numN2 # Modulo division
abs(numN) # Absolute value
使用迭代器:
sysN = cn.CustomNumeralSystem("paf")
it = cn.GearIterator(sysN, 0, 2)
next(it) # "p" # "p" assumes to be the analog of the zero
next(it) # "a"
next(it) # "f"
next(it) # "ap"
next(it) # "aa"
# and so on. You get the idea.
# The iterator could also be initialized with an init_value which is
# de-facto a custom number from the chosen CustomNumeralSystem,
# but for convenience I left the number to be a string, as you may
# wish or not to initialize at all:
it = cn.GearIterator(sysN, 0, 2, "af")
注意:如果初始化,迭代器將從給定的init_value剝離任何領先的“零”(可以說)。
定義並聲明一個自定義數字系統。
CustomNumeralSystem(digits: str)
Args:
digits: The symbols to be used as digits. The string length defines
the numeral system base.
特性:
forbidden_characters -> str
base -> int
方法:
valid_number(number: str) -> bool
Tests if the given "number" is valid for the current numeral system.
Should not contain forbidden characters.
Should contain only characters defined in the numeral system.
定義並從自定義數字系統中聲明數字。
CustomNumber(numeral_system: CustomNumeralSystem, value: str)
Args:
numeral_system: A previously defined custom numeral system.
value: The value (the number itself).
特性:
init_value -> str
Returns the initial value the class was initialized with.
方法:
digit_to_int(digit: str) -> int
Converts the given digit from the custom numeral system to a
decimal integer.
to_decimal() -> int
Converts the current number value to a decimal integer.
從第一個數字開始(零等效)或從給定的init_value開始,迭代自定義數字系統的數量。
短暫地模擬舊齒輪櫃檯,例如舊車里程表。
GearIterator(numeral_system: CustomNumeralSystem, min_length: int = 0, max_length: int = 0, init_value: str = "")
Args:
numeral_system: Custom numeral system. Mind the order of symbols!
min_length: Minimum length, default is zero.
max_length: Maximum length, default is zero - means no limit.
init_value: Value to initialize with.
特性:
combinations -> int
Returns the number of possible combinations (iterations).
該類實現Python上下文管理協議。