Comments (3)
I don't think this is a desired feature, since the same argument could be made for the following example:
>>> "{c:02d}".format(c=100000)
'100000'
This would eliminate the feature to specify the number of expected digits. If you expect an integer of unspecified length, you should probably just use parse.parse("{c:d}", "100")
.
from parse.
I am currently using "image_{c:02d}.png"
when creating images and would like to be able to use the same string when parsing image file names in order to avoid having to maintain two strings ( "image_{c:02d}.png"
and "image_{c:d}.png"
) in sync in my code. Also the format string might provided by the user of my code, in which case, having no direct control on the format string in my code, I would need to write some code to automatically remove any digit between ":" and "d" in the format before passing it to parse.
In general it seems to me that aiming for parse
to be the "inverse" of format
i.e. have parse.parse(f, f.format(**d)).named=d
and f.format(**parse.parse(f,s).named)==s
is a good thing and is the behavior I would expect from parse. If that is not the case I believe it makes it less clear what the expected behavior of parse
is in general.
The first equality is not verified when using f="image_{c:02d}.png"
and d={"c": 100}
.
The second equality is not verified when using f="image_{c:02d}.png"
and s="image_9.png"
.
I would want parse.parse("image_{c:02d}.png","image_9.png")
to give me no match because "image_{c:02d}.png".format(c=9)
gives image_09.png
and not image_9.png
, while at the moment is gives me <Result () {'c': 9}>
from parse.
I like your second point. If the programmer explicitly states that one-digit numbers should be padded with zeros (i.e. 02d
format) the string "9"
should not match.
However, the first inconsistency you point out seems to be an issue of Python's format function with an impossible solution, as this is not solvable for parse without losing the whole ability of being able to specify the expected padding of a number.
But honestly, I think that if you expect three digit numbers, you should specify that in your parse string. This is also a lot less surprising for readers of your code. Basically my argument from above applies here, too. If you as the programmer go out of your way to specify a number which is padded to a width of 2 it should only match strings which contain numbers of that width.
May I ask why you use the string "image_{c:02d}.png"
if c
can get > 99?
from parse.
Related Issues (20)
- Move to `pyproject.toml` instead of `setup.py` for packaging HOT 3
- Add documentation for usage of "extra_types" HOT 3
- Length specification is not properly handled with 'x' format character HOT 1
- strftime formatting HOT 1
- Still maintained? Want help to maintain parse? HOT 1
- Binary hash in pypy changed for release 1.19.0 HOT 7
- Hash mismatch when fetching 1.19.0 HOT 3
- Floating point with zero digits after the comma
- Use patterns inductively doesn't seem to work HOT 1
- Where is located the split between two zones when there is more than one possibility? HOT 2
- Timestamp can be stored differently than parsed
- A very weird performance problem while parsing HOT 2
- Allow using a Parser as a formatter HOT 4
- what is the unused match arg in multiple functions? HOT 1
- Found a possible security concern
- Usage with pandas df.str.extract and .match? HOT 1
- compile and parse return different results HOT 1
- Cardinality support HOT 2
- Search for a certain pattern error at the end of a string HOT 1
- How do I match in the middle of a string? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from parse.