Ранее я делал серию постов про битовые операторы.
Вот вам ещё один наглядный пример как это используется в Python в модуле re.
Чтобы указать флаг для компилятора нам надо указать его после передаваемой строки. Например, добавляем флаг для игнорирования переноса строки.
pattern = re.compile(r"(\w+)+")
words = pattern.search(text, re.DOTALL)
А как указать несколько флагов? Ведь явно будут ситуации когда нам потребуется больше одного. Кто читал посты по битовые операторы уже понял как.
pattern.search(text, re.DOTALL | re.VERBOSE)
А теперь смотрим исходники, что находится в этих атрибутах?
Не удивительно, степени двойки. Почему? Потому что каждое следующее значение это сдвиг единицы влево.
>>> for n in [1, 2, 4, 8, 16, 32, 64, 128, 256]:
>>>
print(bin(n))
0b1
0b10
0b100
0b1000
0b10000
0b100000
0b1000000
0b10000000
0b100000000
Чтобы было понятней, давайте напишем тоже самое но иначе, добавим ведущие нули:
000000001
000000010
000000100
000001000
000010000
000100000
001000000
010000000
100000000
Не понятно что тут происходит? Читай три поста про битовые операторы начиная с этого ➡️https://t.me/pythonotes/45
В общем, это пример применения побитовых операций в самом Python.
Теперь вы знаете Python еще немного лучше)
#tricks#regex#libs
🌎 The rare blue mineral painite was first discovered in Myanmar in 1951 and is so scarce that only a few hundred crystals have ever been found worldwide. Painite contains boron, zirconium, and aluminum, and transparent gem-quality stones are especially valuable to collectors. ✨
#geology⚡#minerals⚡#gemstones
👉subscribe Interesting Planet
👉more Channels
🌎 Red beryl, also called bixbite, is one of the rarest gemstones on Earth and is found almost exclusively in Utah’s Wah Wah Mountains. Its rich red color comes from trace amounts of manganese. Fewer than one red beryl is found for every 150,000 diamonds mined worldwide. ✨
#minerals⚡#gemstones⚡#geology
👉subscribe Interesting Planet
👉more Channels
🌎 Discovered only in Greenland and the Kola Peninsula, rare mineral serendibite forms deep in metamorphic rocks and can display vivid blue-green hues. Gem-quality serendibite is so scarce that a single carat can cost over $2,000. ✨
#minerals⚡#geology⚡#gemstones
👉subscribe Interesting Planet
👉more Channels
🌎 Blue garnet is one of the rarest gemstones on Earth, discovered in Madagascar in the late 1990s. Unlike most garnets, it changes color—from blue-green in daylight to purple-red under incandescent light—due to trace amounts of vanadium absorbing light differently. Only tiny amounts have been found so far, with most crystals under one carat. ✨
#geology⚡#minerals⚡#gemstones
👉subscribe Interesting Planet
👉more Channels
🌎 One of the rarest crystals on Earth is musgravite, first discovered in Australia’s Musgrave Ranges in 1967. Transparent musgravite gemstones are extremely scarce, with fewer than 20 certified specimens reported worldwide. ✨
#minerals⚡#gemstones⚡#geology
👉subscribe Interesting Planet
👉more Channels
🌎 The mineral grandidierite is one of the world’s rarest gemstones and was first discovered in Madagascar in 1902. Its blue-green crystals get their striking color from traces of iron, and transparent gem-quality stones are so scarce that most museums only display tiny specimens. ✨
#minerals⚡#gemstones⚡#geology
👉subscribe Interesting Planet
🌎 The mineral taaffeite was discovered accidentally in 1945 by gemologist Richard Taaffe, who bought a mislabeled gemstone in Dublin. This rare crystal is prized for its lavender color and is mostly found in Sri Lanka, Myanmar, and Madagascar. Fewer than 50 verified cut taaffeites exist worldwide. ✨
#minerals⚡#gemstones⚡#geology
👉subscribe Interesting Planet
🌎 An ancient “rainbow” mineral called ammolite forms from fossilized ammonite shells. With its dazzling reds, greens, and blues, ammolite is one of the world’s rarest gemstones, found mainly in Canada’s Rocky Mountains. ✨
#geology⚡#fossils⚡#gemstones
👉subscribe Interesting Planet
🌎 In Australia’s opal fields, some opals contain ancient fossilized bones or shells, perfectly preserved in dazzling rainbow stone. This rare “opalized fossil” forms as minerals slowly replace bone, creating a sparkling record of prehistoric life. ✨
#fossils⚡#minerals⚡#gemstones
👉subscribe Interesting Planet
🌎 The mineral jeremejevite forms pale blue, yellow, or colorless crystals and is among the world’s rarest minerals. Discovered in Namibia in 1883, it is an aluminum borate mineral that forms in granite pegmatites. Transparent gem-quality jeremejevite is so scarce that it can sell for over $2,000 per carat. ✨
#minerals⚡#crystals⚡#gemstones
👉subscribe Interesting Planet
🌎 Alexandrite is a rare mineral that changes color under different lighting—appearing green in daylight and red under incandescent light. This gemstone was first discovered in Russia’s Ural Mountains in 1830, and crystals large enough for jewelry are extremely scarce worldwide. ✨
#alexandrite⚡#minerals⚡#gemstones
👉subscribe Interesting Planet
👉more Channels