Experimental utilities for Python
Project description
$Release: 0.1.0 $
Useful tools for Python.
benry.rexp
rx()
benry.rexp.rx() is a short cut to 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)
You don’t need to use re.xxxx() functions because rx().xxxx() does same things, and has more features.
## 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() (or benry.rexp.compile()) is similar to re.compile(), but the former doesn’t cache compiled pattern while the latter caches it.
This is very useful when there are a lot of regexp pattern and they are no need to cache into library.
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() (or benry.rexp.matching()) is a utility class to help you when matching to a lot of patterns.
Without 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()
With 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
You can get SRE_Match object by m.matched.
m = rx.matching(string) if m.match(r'pat'): print(m.matched.pos) print(m.matched.endpos)
type
rx.type represents class of regular expression.
import re from benry.rexp import rx assert rx.type is type(re.compile('x'))
benry.date_time
class UTCDateTime
UTCDdateTime is a subclass of datetime.datetime representing UTC offset.
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.
class LocalDateTime
UTCDdateTime is a subclass of datetime.datetime representing local time. This class calculates offset between local time and UTC time.
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file benry-0.1.0.tar.gz
.
File metadata
- Download URL: benry-0.1.0.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a52d0049127a2a4aa65c9532ee3d67e19535cc120edf0bb1c93e7195f95f2c0b |
|
MD5 | 3482d409888f2b3c6c5a1b4dbf1f8548 |
|
BLAKE2b-256 | bb7b8db3f4057471131572aa45b2f3f32c79bd096eba2369c3f0fa9e665eaf52 |