{ "info": { "author": "Edwin van Opstal", "author_email": "evo.se-technology.com", "bugtrack_url": null, "classifiers": [], "description": "==================================================\npyutillib - A library of general utility functions\n==================================================\n\nPython Utility Library is a small collection of functions in several categories.\n\nDate functions\n==============\n\nIn order to use a date function you need to::\n\n import pyutillib.date_utils\n\nWorking with date strings\n-------------------------\n\nIn order to have an unambiguous relation between a date in string and datetime\nformat, only a limited number of formats is available. The default is often \ngood enough.\n\nUsage examples::\n\n >>> import pyutillib.date_utils as du\n >>> du.datestr2date('20001231')\n datetime.date(2000, 12, 31)\n >>> du.datestr2date('12/31/2000')\n datetime.date(2000, 12, 31)\n >>> du.datestr2date('31-12-2000')\n datetime.date(2000, 12, 31)\n\n >>> import datetime\n >>> d = datetime.date(2000, 12, 31)\n >>> du.date2datestr(d)\n '20001231'\n >>> du.date2datestr(d, 'd-m-yy')\n '31-12-00'\n >>> du.date2datestr(d, 'd-m-yyy')\n Traceback (most recent call last):\n File \"\", line 1, in \n File \"pyutillib/date_utils.py\", line 128, in date2datestr\n raise ValueError('Invalid format string, year must have 2 or 4 digits')\n ValueError: Invalid format string, year must have 2 or 4 digits\n >>> du.date2datestr(d, 'mm/dd/yyyy')\n '12/31/2000'\n >>> du.date2datestr(d, 'yymmdd')\n '001231'\n\nWorking with weekdays\n---------------------\n\nThese are simple things, but hard to remember, so just a few convenience \nfunctions::\n\n >>> import pyutillib.date_utils as du\n >>> import datetime as dt\n >>> du.is_weekday(dt.date(2013,4,16))\n True\n >>> du.is_weekday(dt.date(2013,4,14))\n False\n >>> du.is_weekend(dt.date(2013,4,14))\n True\n >>> du.is_weekend(dt.date(2013,4,16))\n False\n >>> du.next_weekday(dt.date(2013,4,16))\n datetime.date(2013, 4, 17)\n >>> du.next_weekday(dt.date(2013,4,12))\n datetime.date(2013, 4, 15)\n >>> du.next_weekday(dt.date(2013,4,13))\n datetime.date(2013, 4, 15)\n >>> du.previous_weekday(dt.date(2013,4,16))\n datetime.date(2013, 4, 15)\n >>> du.previous_weekday(dt.date(2013,4,15))\n datetime.date(2013, 4, 12)\n >>> du.previous_weekday(dt.date(2013,4,14))\n datetime.date(2013, 4, 12)\n\nWorking with years\n------------------\n\nSimple, but takes leap years into account::\n\n >>> import pyutillib.date_utils as du\n >>> import datetime as dt\n >>> du.last_year(dt.date(2000,2,29))\n datetime.date(1999, 2, 28)\n >>> du.last_year(dt.date(2001,2,29))\n Traceback (most recent call last):\n File \"\", line 1, in \n ValueError: day is out of range for month\n >>> du.last_year(dt.date(2001,2,28))\n datetime.date(2000, 2, 28)\n >>> du.last_year(dt.date(2001,3,1))\n datetime.date(2000, 3, 1)\n\nThe DateList class\n------------------\n\nThis class subclasses the standard Python list, so all normal list functionality\nis available. The differences are that there are a few extra methods and that\nthe .index method works differently.\n\n**Instantiating:** a list of dates works as expected::\n\n >>> import pyutillib.date_utils as du\n >>> import datetime as dt\n >>> dates = [dt.date(2012, 1, d) for d in range(1, 32)]\n >>> dl = du.DateList(dates)\n >>> dl[0]\n datetime.date(2012, 1, 1)\n >>> dl[-1]\n datetime.date(2012, 1, 31)\n\n**index** returns the the index of a date, or (if the date is not in the list), \nthe index of the most recent date before the input date::\n\n >>> dl.index(dt.date(2012,1,2))\n 1\n >>> dl.index(dt.date(2012,1,30))\n 29\n >>> dates2 = [dt.date(2012, 1, d) for d in range(1, 32, 4)]\n >>> dl2 = du.DateList(dates2)\n >>> for d in dl2: print d\n 2012-01-01\n 2012-01-05\n 2012-01-09\n 2012-01-13\n 2012-01-17\n 2012-01-21\n 2012-01-25\n 2012-01-29\n >>> dl2.index(dt.date(2012,1,8))\n 1\n >>> dl2.index(dt.date(2012,1,9))\n 2\n >>> dl2.index(dt.date(2012,1,10))\n 2\n\n**on_or_before** returns the input date, or (if the date is not in the list), \nthe most recent date before the input date::\n\n >>> dl2.on_or_before(dt.date(2012,1,5))\n datetime.date(2012, 1, 5)\n >>> dl2.on_or_before(dt.date(2012,1,4))\n datetime.date(2012, 1, 1)\n >>> dl2.on_or_before(dt.date(2012,1,6))\n datetime.date(2012, 1, 5)\n\n**delta** returns the number of days in the list between two dates::\n\n >>> dl.delta(dt.date(2012,1,10), dt.date(2012,1,20))\n 10\n >>> dl2.delta(dt.date(2012,1,10), dt.date(2012,1,20))\n 2\n\n**offset** returns the date n_days after (or before if n_days < 0) the input\ndate, note that these are not calendar days, but dates in the list::\n\n >>> dl.offset(dt.date(2012,1,10),3)\n datetime.date(2012, 1, 13)\n >>> dl2.offset(dt.date(2012,1,10),3)\n datetime.date(2012, 1, 21)\n\n**subset** returns a list of dates between two specified dates, only dates that\nare in the original list are included::\n\n >>> for d in dl.subset(dt.date(2012,1,10), dt.date(2012,1,20)): print d\n 2012-01-10\n 2012-01-11\n 2012-01-12\n 2012-01-13\n 2012-01-14\n 2012-01-15\n 2012-01-16\n 2012-01-17\n 2012-01-18\n 2012-01-19\n 2012-01-20\n >>> for d in dl2.subset(dt.date(2012,1,10), dt.date(2012,1,20)): print d\n 2012-01-13\n 2012-01-17\n\nWorking with time strings\n-------------------------\n\nIn order to have an unambiguous relation between a time in string and \ndatetime.time format, only a limited number of formats is available. The \ndefault is often good enough.\n\nUsage examples::\n\n >>> from pyutillib import date_utils as du\n >>> du.timestr2time('123456')\n datetime.time(12, 34, 56)\n >>> du.timestr2time('12:34:56')\n datetime.time(12, 34, 56)\n >>> du.timestr2time('12:34')\n datetime.time(12, 34)\n >>> du.timestr2time('01:23')\n datetime.time(1, 23)\n >>> du.timestr2time('1:23')\n datetime.time(1, 23)\n \n >>> import datetime\n >>> t = datetime.time(23,59,59)\n >>> du.time2timestr(t)\n hhmmss 23:59:59\n '235959'\n >>> du.time2timestr(t, 'hh:mm')\n hh:mm 23:59:59\n '23:59'\n >>> du.time2timestr(t, 'hh:mm:ss')\n hh:mm:ss 23:59:59\n '23:59:59'\n >>> t = datetime.time(5,59,59)\n >>> du.time2timestr(t)\n hhmmss 05:59:59\n '055959'\n >>> du.time2timestr(t, 'hh:mm:ss')\n hh:mm:ss 05:59:59\n '05:59:59'\n >>> du.time2timestr(t, 'h:mm:ss')\n h:mm:ss 05:59:59\n '5:59:59'\n >>> du.time2timestr(t, 'h:mm')\n h:mm 05:59:59\n '5:59'\n >>> du.time2timestr(t, 'hmmss')\n hmmss 05:59:59\n Traceback (most recent call last):\n File \"\", line 1, in \n File \"pyutillib/date_utils.py\", line 383, in time2timestr\n else:\n ValueError: Invalid character in format string. The following time formats are valid:\n hhmmss\n hh:mm:ss h:mm:ss\n hh:mm h:mm\n Where in the latter 2 formats hh has d digits which may include a leading zero\n and h may have 1 or 2 digits and no leading zero. \n h/hh is always in 24 hour clock.\n\nMath functions\n==============\n\nIn order to use a math function you need to::\n\n import pyutillib.math_utils\n\nSafe division\n-------------\n\nSometimes you need to divide 2 numbers without worrying about division by zero.\nthe ``div`` function always returns a float, even if there is an iteger result.\nAlso the result of 0/0 is not mathematically correct, but often practically OK.\n\nSome examples::\n\n >>> from pyutillib import math_utils as mu\n >>> mu.div(1,0)\n inf\n >>> mu.div(1,1)\n 1.0\n >>> mu.div(0,0)\n 0.0\n\nEvaluating conditions\n---------------------\n\nThis function can be used to evaluate conditions of arbitrary complexity. The\nconditions need to be in a tuple format::\n\n (argument1, operator, argument2)\n\nWhere either argument 1 and 2 can be booleans if operator is a logical operator\n('and', 'or'), or argument 1 and 2 are python objects if operator is a comparison\noperator ('lt', 'le', 'eq', 'ne', 'ge', 'gt'). In the latter case both arguments\nmust be comparable, i.e. be of the same type. The only exception are floats and\nints, they can be compared with each other.\n\nThe outcome of each evaluation is always a boolean and they can be nested to\nany level you like, by replacing a boolean argument by another tuple, e.g.::\n\n >>> from pyutillib import math_utils as mu\n >>> mu.eval_conditions(((6, 'gt', 5.7), 'and', True))\n True\n\nIt is possible to provide arguments by name, if you specify their value in a\ndict::\n\n >>> arg_dict = {'a': 11, 'b': 0.24}\n >>> condition = ('a', 'eq', 'b')\n >>> mu.eval_conditions(condition, arg_dict)\n False\n\nString functions\n================\n\nIn order to use a string function you need to::\n\n import pyutillib.string_utils\n\nGenerating a random string\n--------------------------\n\nThis function generates a random string of specified length made up of letters\nand digits. A custom character set can be specified to limit (or extend) the\ncollection::\n\n >>> from pyutillib import string_utils as su\n >>> su.random_string()\n '7xgVQZxd'\n >>> su.random_string(charset='ABC+-')\n '-+-A+CBA'\n >>> su.random_string(20, 'ABC+-')\n 'A+AB--BCB++CA-A++++C'\n\nSafely evaluating strings\n-------------------------\n\nInstead of using eval, ast provides a better (safer) alternative. This function\nis just a wrapper around that function to avoid exceptions::\n\n >>> su.safe_eval('(2,3,4)')\n (2, 3, 4)\n >>> print su.safe_eval('import os; os.name')\n None\n\nWorking with tuples and dicts in string format\n----------------------------------------------\n\nAll functions below return None if the input string does not have the required\nformat.\n\nExtracting a tuple from a string::\n\n >>> print su.str2tuple('(1,2,3)')\n (1, 2, 3)\n >>> print su.str2tuple('[1,2,3]')\n None\n >>> print su.str2tuple('hallo')\n None\n\nExtracting a dict from a string::\n\n >>> print su.str2dict('{1:2, 3:4}')\n {1: 2, 3: 4}\n >>> print su.str2dict(' {1:2, 3:4}')\n None\n\nGetting the keys from a dict in a string. The keys will be returned in\nalphabetic order::\n\n >>> print su.str2dict_keys('{\"a\":1, 2:\"3\", -1: 0}')\n [-1, 2, 'a']\n >>> print su.str2dict_values('{\"a\":1, 2:\"3\", -1: 0}')\n [0, '3', 1]\n\nTranslating a decimal string to an int\n--------------------------------------\n\nThis function takes a string that represents a decimal number and returns an \ninteger. The *decimals* argument allows you to 'shift the decimal point', e.g.::\n\n >>> from pyutillib import string_utils as su\n >>> su.decstr2int('123.456', 3)\n 123456\n >>> su.decstr2int('123.456', 2)\n 12345\n >>> su.decstr2int('123.456', 4)\n 1234560\n >>> su.decstr2int('123', 4)\n 1230000\n >>> su.decstr2int('123.456', -1)\n 12\n\nNote from the above examples that the input string does not need to contain a \ndecimal point and also the decimals argument may be negative (or 0).", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "http://github.com/EdwinvO/pyutillib", "keywords": null, "license": "LICENSE.txt", "maintainer": null, "maintainer_email": null, "name": "pyutillib", "package_url": "https://pypi.org/project/pyutillib/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/pyutillib/", "project_urls": { "Download": "UNKNOWN", "Homepage": "http://github.com/EdwinvO/pyutillib" }, "release_url": "https://pypi.org/project/pyutillib/0.3.0/", "requires_dist": null, "requires_python": null, "summary": "Small collection of python functions", "version": "0.3.0" }, "last_serial": 798233, "releases": { "0.1.0": [ { "comment_text": "", "digests": { "md5": "5659ef6a2e6e6e87cf8ccc00131ff55f", "sha256": "f5019e60a46f35f5d02215c46124cae32d459118c880bb348ef4bb3706ae54a6" }, "downloads": -1, "filename": "pyutillib-0.1.0.tar.gz", "has_sig": false, "md5_digest": "5659ef6a2e6e6e87cf8ccc00131ff55f", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 19172, "upload_time": "2013-04-16T03:43:02", "url": "https://files.pythonhosted.org/packages/d3/cd/0d5cdb2790d790e76ee50f4cae0a0756ab00cbaa86497cf7289c3fc87f2e/pyutillib-0.1.0.tar.gz" } ], "0.1.1": [ { "comment_text": "", "digests": { "md5": "01498aa623f89780f33e551d66d038a0", "sha256": "7d0ecd420eca06198fd981d76a58b878263855f68a14a68ba6ce0cb608f81024" }, "downloads": -1, "filename": "pyutillib-0.1.1.tar.gz", "has_sig": false, "md5_digest": "01498aa623f89780f33e551d66d038a0", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 22031, "upload_time": "2013-04-16T04:39:31", "url": "https://files.pythonhosted.org/packages/23/d8/7e6cecfdee64e679959e4de38d4fcb487db37b374707d082df8e8dd92f85/pyutillib-0.1.1.tar.gz" } ], "0.2.0": [ { "comment_text": "", "digests": { "md5": "6480dc1fe0ae6c2297227bce502a1e8d", "sha256": "79d7a655f618548d78b51d558152a353c1df30b1dbd59e651f65f5bb2bac042e" }, "downloads": -1, "filename": "pyutillib-0.2.0.tar.gz", "has_sig": false, "md5_digest": "6480dc1fe0ae6c2297227bce502a1e8d", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 24913, "upload_time": "2013-04-24T05:59:01", "url": "https://files.pythonhosted.org/packages/f4/2d/e9c2ead43a8ff20d5cb862081ee1ee2ca70df85f9729f0d99a3ca7f037f6/pyutillib-0.2.0.tar.gz" } ], "0.3.0": [ { "comment_text": "", "digests": { "md5": "37b6b1ee939cdfd9ced0f94c87bde7d1", "sha256": "5dd06b815dd5af0ed43da7e137ad74db45d9847f3a482ca3da667a14daf19379" }, "downloads": -1, "filename": "pyutillib-0.3.0.tar.gz", "has_sig": false, "md5_digest": "37b6b1ee939cdfd9ced0f94c87bde7d1", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 27458, "upload_time": "2013-05-01T06:05:40", "url": "https://files.pythonhosted.org/packages/4f/69/811fd443164a5b6f338a57ce56c1db10d0e06f2cc1fda2aa7265712d98e5/pyutillib-0.3.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "37b6b1ee939cdfd9ced0f94c87bde7d1", "sha256": "5dd06b815dd5af0ed43da7e137ad74db45d9847f3a482ca3da667a14daf19379" }, "downloads": -1, "filename": "pyutillib-0.3.0.tar.gz", "has_sig": false, "md5_digest": "37b6b1ee939cdfd9ced0f94c87bde7d1", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 27458, "upload_time": "2013-05-01T06:05:40", "url": "https://files.pythonhosted.org/packages/4f/69/811fd443164a5b6f338a57ce56c1db10d0e06f2cc1fda2aa7265712d98e5/pyutillib-0.3.0.tar.gz" } ] }