Giter Site home page Giter Site logo

Comments (3)

sueskind avatar sueskind commented on August 11, 2024

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.

martinResearch avatar martinResearch commented on August 11, 2024

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.

sueskind avatar sueskind commented on August 11, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.