Package: aiotense
Project: aiotenseLicense: Apache 2.0About: Time Processing ToolOS: IndependentPython: 3.9+Typing: TypedTopic: UtilitiesDocumentation·Report Bug·Request FeatureTable of ContentsAbout The ProjectWelcomeGetting StartedWith PyPiWith PoetryUsageBuilt-in basicsReconfiguring existing settingsAdding new settingsFAQExamplesContributingLicenseContactAcknowledgmentsAbout The ProjectWelcomeHave you ever needed to convert, for example, the string "1d1minute 2 sec"
to the number of seconds or a datetime.timedelta object?No? Then advise us to your friends :) And if you really need our tool - let's move on!Getting startedWith PyPi$pip3installaiotenseWith Poetry$poetryaddaiotenseUsageBuilt-in basicimportasyncioimportdatetimefromaiotenseimportTenseParsertime_string="1d2minutes 5 sec"# <-- Digit parser -->digit_parser=TenseParser(TenseParser.DIGIT)digit_value=asyncio.run(digit_parser.parse(time_string))# <-- Assertions -->assertdigit_value==86525# <-- Timedelta parser -->delta_parser=TenseParser(TenseParser.TIMEDELTA)delta_value=asyncio.run(delta_parser.parse(time_string))# <-- Assertions -->assertisinstance(delta_value,datetime.timedelta)assertstr(delta_value)=="1 day, 0:02:05"Reconfiguring existing settingsimportasynciofromaiotenseimportTenseParser,from_tense_file_sourceconfig_emulation="""[model.Tense]multiplier = 2  # each unit of time will be multiplied by 2# !!! Note: If the multiplier is <= 0, then the parsers will# not work correctly. In this case, a warning will be sent to the console.[units.Minute]duration = 120  # Why not?...aliases = my_minute, my_minutes, my_min, my_mins"""parser=TenseParser(TenseParser.TIMEDELTA,config=from_tense_file_source(config_emulation),)delta_value=asyncio.run(parser.parse("1 my_min 10my_mins 9  my_minutes"))# <-- Assertions -->assertstr(delta_value)=="1:20:00"# (each 120 * 2)Adding new settingsimportasynciofromaiotenseimportTenseParser,from_tense_file_sourceconfig_emulation="""[model.Tense]  # This header is required.[virtual]duration = exp(year * 10)aliases = decade, dec, decs, decades"""parser=TenseParser(TenseParser.TIMEDELTA,config=from_tense_file_source(config_emulation),)delta_value=asyncio.run(parser.parse("1year 10 decades5   seconds"))# <-- Assertions -->assertstr(delta_value)=="36865 days, 0:00:05"FAQBut what if you need to parse a string like: "1day and 10 minutes + 5 seconds"?
Let's see:>>>importasyncio>>>fromaiotenseimportTenseParser>>>complex_string="1day and 10 minutes + 5 seconds">>>parser=TenseParser(TenseParser.TIMEDELTA)>>>asyncio.run(parser.parse(complex_string))'0:00:05'Wait... What? 5 second? But there are days and minutes...It's okay, you're using flexible aiotense! This problem is solved in two ways:You write your own time_resolver and pass itChoose an existing one from aiotense.resolversLet's demonstrate!
I will use the second option, since the built-in time resolvers in aiotense are suitable for me.>>>importasyncio>>>fromaiotenseimportTenseParser,resolvers>>>complex_string="1day and 10 minutes + 5 seconds">>>parser=TenseParser(TenseParser.TIMEDELTA,time_resolver=resolvers.smart_resolver)>>>asyncio.run(parser.parse(complex_string))'1 day, 0:10:05'Well, that's better!aiotense.application.resolvers.smart_resolver()is also case insensitive!>>>importasyncio>>>fromaiotenseimportTenseParser,resolvers>>>complex_string="1DAY and 10 MINUTES + 5 SECONDS">>>parser=TenseParser(TenseParser.TIMEDELTA,time_resolver=resolvers.smart_resolver)>>>asyncio.run(parser.parse(complex_string))'1 day, 0:10:05'Examples.If you think that this is where the possibilities of aiotense ends, then you are wrong!
The possibilities of aiotense are too many for a README, so I suggest you move on to viewing
the usage examples here:Aiotense ExamplesContributingContributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make aregreatly appreciated.If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement".
Don't forget to give the project a star! Thanks again!Fork the ProjectCreate your Feature Branch (git checkout -b feature/AmazingFeature)Commit your Changes (git commit -m 'Add some AmazingFeature')Push to the Branch (git push origin feature/AmazingFeature)Open a Pull RequestLicenseDistributed under the Apache 2.0 License. SeeLICENSEfor more information.ContactAcknowledgmentsChoose an Open Source LicenseImg ShieldsGitHub PagesPythonPython CommunityMkDocsMkDocs Material