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.