Функция sub в regex может принимать функцию в качестве аргумента repl.
📄 Из документации:
If repl is a function, it is called for every non-overlapping occurrence of pattern. The function takes a single match object argument, and returns the replacement string.
То есть для каждого совпадения будет вызвана функция для вычисления замены вместо замены на одну и ту же строку для всех совпадений.
Иными словами, для замены разных совпадений на разные строки не потребуется запускать re.sub() много раз для каждой строки замены. Достаточно определить функцию, которая вернёт строку для каждого из совпадений.
Описание слишком запутанное🤔, давайте лучше рассмотрим на простом примере:
Создаем карту замены. То есть какие строки на какие требуется менять.
remap = {
'раз': '1',
'два': '2',
'три': '3',
'четыре': '4',
'пять': '5',
}
Пишем функцию поиска строки для замены. Единственным аргументом будет объект re.Match.
Используя данные этого объекта мы вычисляем замену on-the-fly!
def get_str(match: re.Match):
word = match.group(1)
return remap.get(word.lower()) or word
Пример текста.
text = '''Раз Два Три Четыре Пять
Вместе будем мы считать
Пять Четыре Три Два Раз
Мы считать научим вас
'''
Теперь запускаем re.sub и вместо строки замены (repl) подаём имя функции.
(Данный паттерн ищет отдельные слова в тексте)
>>> print(re.sub(r'(\w+)', get_str, text))
1 2 3 4 5
Вместе будем мы считать
5 4 3 2 1
Мы считать научим вас
Думаю, достаточно наглядно 🤓
#libs#regex
⚡️TON — LIVE: Launch of First On-chain DAO by STON.fi
#TON#STON
TON — LIVE reports that STON.fi has launched the first fully functional on-chain DAO in the TON ecosystem. Staked STON tokens can now be used for decision-making and influencing protocol development.
Source: link
@tonlines
⚡️STON.fi: Launch of the First DAO on TON
#TON#STON
STON.fi has ushered in a new decentralized era on TON with the launch of its first full-fledged on-chain DAO. This governance model allows community members to use their staked STON for voting power on STON.fi’s future developments.
Source: link
@tonlines
⚡️STON.fi: Celebrating Three Years in DeFi
#TON#STON
STON.fi celebrated its third anniversary, reflecting on its growth from 348 users to 5.6M and a trading volume increase from $92K to $6.65B. The week saw the launch of community contests, new cross-chain swapping capabilities, and a successful wrap-up of the DAO Genesis testing phase.
Source: link
@tonlines
STON.fi Weekly Round-Up: Highlights and Achievements
#STON#TON
STON.fi reflects on a week of growth and innovation, with nearly 495,000 swaps occurring in the last month. Noteworthy events include a successful meetup in Prague, the conclusion of the 42-Hunt contest, and winners announced for the Vibe Coding Hackathon. Additionally, MusiKhan's use of the STON.fi SDK demonstrates the platform's real-world utility.
Source: link
@tonlines
⚡️swap.coffee Integrates WSS Pools from STON.fi
#STON#TON
swap.coffee has become the first DEX aggregator to support Weighted Stable Swap pools by STON.fi. This innovation offers enhanced stability and reduced slippage for assets in the TON ecosystem.
Source: link
@tonlines
⚡️STON.fi: Launch of a $500,000 Bug Bounty Program
#STON.fi #TON
STON.fi has unveiled a Bug Bounty Program for its v2 smart contracts with a reward pool of $500,000. Collaborating with HackenProof and Certik, the channel seeks to enhance security by inviting the community to uncover vulnerabilities and improve its asset swapping platform on TON. All tests should be conducted on the testnet for safety.
Source: link
@tonlines
STON.fi: Introducing the Exclusive STON.fi Club
#Telegram#STON
STON.fi announces the launch of the STON.fi Club, a private community on Telegram for verified market players including STON token stakers, holders, and liquidity providers. This club offers direct communication with the team, early insights on product changes, and market commentary, fostering a dynamic exchange that shapes the future of the STON.fi ecosystem.
Source: link
@tonlines
🎰🎰🎰Major update
The DEX team announced the launch of STON.fi V2 with many new features and plans to improve their product. STON.fi smart contracts have been updated for the first time in 2 years!
🎁The following features are already available:
👍 Create a liquidity pool and add liquidity to it with one transaction. Now sniper bots will not be able to spoil the proportion of tokens in the pool to influence the rate. And the new interface of the Pools tab made any operations with liquidity as simple as possible.
👍 Flexible referral commissions in STON.fi SDK. Developers will surely appreciate this feature.
🙂But that's not all, there are plans to add even more cool features in the future:
👍 Routing is what every STON.fi fan is waiting for. Exchange any token for any other even if they do not have a common liquidity pair, the algorithm will make the exchange as profitable as possible!
👍 New unique types of liquidity pools: stable pools, as well as a pool with an arbitrary proportion of tokens (for example, 8 to 2)
👍 Liquidity supply and withdrawal in one token instead of two, as well as liquidity withdrawal in an arbitrary token ratio — all this will appear on STON.fi very soon!
👍 Supply liquidity and send it to farming in one transaction. It will be twice as fast and cheaper than now.
🪨Try the new STON.fi V2
The team has set truly ambitious goals for themselves. We believe that they will succeed!
#STON#TON#DEX#DeFi
⚡️Toncoin: Latest Updates in TON Ecosystem
#TON#STON#crypto
Toncoin channel shares the latest updates across the TON ecosystem, including Tonkeeper Pro enhancements, STON.fi SDK integration, MyTonWallet 4.3 features, and SIXR Cricket partnership involving cricket legends. These updates aim to improve user experience and expand TON’s reach.
Source: link
@tonlines
⚡️STON.fi: Launch of WSS Pools and Farming Opportunity
#STON#DeFi#TON
STON.fi has unveiled its new Weighted Stable Swap (WSS) pools within the TON DeFi ecosystem, starting with the tsTON/TON pair. Users can participate in an exclusive farming opportunity with 1000 STON tokens rewards, available until June 6, 2025.
Source: link
@tonlines
📈Bybit Web3: Championship and 4295 STON airdrop
🎰🎰🎰
This month, DEX Ston.fi was added as a participant in the championship from the crypto wallet Bybit. The Bybit web3 championship is a great chance to get acquainted with the capabilities of the wallet and participate in the prize draw from STON.fi and many other blockchain projects.
The goal of the championship is to introduce participants to the projects and capabilities of the TON ecosystem.
Your task is to compete with others, complete tasks and receive prizes from projects.
How to participate?
1️⃣ Go to the championship page.
2️⃣ Connect your Bybit wallet or create a new one.
3️⃣ Top up your wallet with 8 TON (this amount is recommended).
4️⃣ Select tasks from the projects you like and click the Start button.
🎈Prize pool
4295 $STON (~$25,000) will be distributed among those who exchanged any tokens worth $10 or more on STON.fi.
🗓 You can participate until October 10th 7:00 UTC
#STON#Bybit#airdrop#DEX
⚡️STON.fi: Impressive Swapping Performance in October
#STON#DEX
STON.fi reported an astounding $120 million swapped in October, highlighting its fast and reliable infrastructure. This channel demonstrates its capability to handle high volumes swiftly, offering users exceptional swap speed.
Source: link
@tonlines