python的实验实用程序

benr的Python项目详细描述


$release:0.1.0$

python的有用工具。

本尼·雷克斯

Rx()

benry.rexp.rx()re.compile()的捷径。

from benry.rexp import rx

## comping -- same as re.compile(r'pat') or re.compile(r'pat', rx.I|rx.S)
regexp = rx(r'pat')
regexp = rx(r'pat', rx.I|rx.S)

## matching -- same as re.compile(r'pat').search(string)
m = rx(r'pat').search(string)

## replacing -- same as re.compile(r'pat').sub('repl', string, count=1)
rx(r'pat').sub('repl', string, count=1)

你不需要使用re.xxxx()函数,因为rx().xxxx()做同样的事情, 有更多的特点

## For example you can't specify starting/ending position with re.match().
re.match(r'pat', string, re.I)

## But you can specify them with rx().match().
rx(r'pat', re.I).match(string, start_pos, end_pos)

rx.compile()

rx.compile()(或benry.rexp.compile())类似于re.compile(), 但是前者不缓存已编译的模式,而后者缓存它。

当有很多regexp模式时这非常有用 不需要缓存到库中

mappings = [
    (r'^/posts$',                      'app.PostsPage'),
    (r'^/posts/new$',                  'app.PostCreatePage'),
    (r'^/posts/(?P<id>\d+)$',          'app.PostPage'),
    (r'^/posts/(?P<id>\d+)/edit$',     'app.PostUpdatePage'),
    (r'^/posts/(?P<id>\d+)/comments$', 'app.PostCommentsPage'),
]

## no need to cache patterns, so calls rx.compile() instead of re.compile()
from benry.rexp import rx   # or: import compile
compiled_mappings = [ (rx.compile(pat), cls) for pat, cls in mappings ]

rx.matching()

rx.matching()(或benry.rexp.matching())是帮助您的实用程序类 当与许多模式匹配时。

没有rx.matching()

import re

m = re.match(r'^(\d\d\d\d)-(\d\d)-(\d\d)$', string)
if m:
    Y, M, D = m.groups()
else:
    m = re.match(r'^(\d\d)/(\d\d)/(\d\d\d\d)$', string)
    if m:
        M, D, Y = m.groups()
    else:
        m = re.match(r'^(\d\d\d\d)/(\d\d)/(\d\d)$', string)
        if m:
            Y, M, D = m.groups()

使用rx.matching()

from benry.rexp import rx

m = rx.matching(string)
if   m.match(r'^(\d\d\d\d)-(\d\d)-(\d\d)$'):
    Y, M, D = m.groups()     # or Y, M, D = m
elif m.match(r'^(\d\d)/(\d\d)/(\d\d\d\d)$'):
    M, D, Y = m.groups()     # or M, D, Y = m
elif m.match(r'^(\d\d\d\d)/(\d\d)/(\d\d)$'):
    Y, M, D = m.groups()     # or Y, M, D = m

您可以通过m.matched获得SRE_Match对象。

m = rx.matching(string)
if m.match(r'pat'):
    print(m.matched.pos)
    print(m.matched.endpos)

类型

rx.type表示正则表达式的类。

import re
from benry.rexp import rx

assert rx.type is type(re.compile('x'))

benry.日期时间

UtcDateTime类

UTCDdateTime是表示UTC偏移量的datetime.datetime的子类。

from benry.date_time import UTCDateTime

print(UTCDateTime.offset_minutes)      #=> 0
print(UTCDateTime.offset_timedelta)    #=> timedelta(seconds=0)
print(UTCDateTime.is_utc)              #=> True
print(UTCDateTime.is_local)            #=> False

## almost same as datetime.utcnow(), except returning UTCDateTime object.
utc_dt = UTCDateTime.now()

print(utc_dt.to_utc())                 # returns self.
print(utc_dt.to_local())               # returns LocalDateTime object.

类LocalDateTime

UTCDdateTime是代表本地时间的datetime.datetime的子类。 此类计算本地时间和UTC时间之间的偏移量。

from benry.date_time import LocalDateTime

print(LocalDateTime.offset_minutes)    #=> 9*60  (ex: JST timezone)
print(LocalDateTime.offset_timedelta)  #=> timedelta(seconds=9*60*60)
print(LocalDateTime.is_utc)            #=> False
print(LocalDateTime.is_local)          #=> True

## almost same as datetime.now(), except returning LocalDateTime object.
local_dt = LocalDateTime.now()

print(local_dt.to_utc())               # returns UTCDateTime object.
print(local_dt.to_local())             # returns self.

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java什么会导致程序在它似乎拥有的监视器上被阻止?   java Android studio设置视图的背景色   java我可以保存一个文本文件而不给用户修改它的能力吗?   pdfbox PDFBOX2。0:java堆堆栈错误   java是维护和操作AllowList的有效方法   JAVAsql。SQLException:找不到适合jdbc的驱动程序:mysql://localhost:3306/asd性爱   如何使用java。lang.NullPointerException:void 安卓。支持v7。应用程序。ActionBar。setElevation(float)“”在空对象引用上'   java调试空指针异常   java正则表达式,以按令牌的特定匹配项拆分,同时忽略其他匹配项   java为JPanel设置边框上的笔划   并发@Schedule方法的java行为   如何在Java中使用泛型与语言运算符和泛型类扩展数   java Rhino Javascript如何为异常堆栈跟踪标记字符串源   运行可执行jar时发生java错误,无法找到或加载主类