{"id":15009969,"url":"https://github.com/ronenness/grepfunc","last_synced_at":"2025-04-09T17:52:50.341Z","repository":{"id":57436008,"uuid":"86633113","full_name":"RonenNess/grepfunc","owner":"RonenNess","description":"Simple grep-like function for Python.","archived":false,"fork":false,"pushed_at":"2021-10-09T19:54:58.000Z","size":25,"stargazers_count":9,"open_issues_count":2,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-23T19:51:33.361Z","etag":null,"topics":["python","python-","python-2","python-3","regex","search","string"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RonenNess.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-03-29T21:57:19.000Z","updated_at":"2023-02-04T16:12:13.000Z","dependencies_parsed_at":"2022-09-01T18:41:57.558Z","dependency_job_id":null,"html_url":"https://github.com/RonenNess/grepfunc","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RonenNess%2Fgrepfunc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RonenNess%2Fgrepfunc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RonenNess%2Fgrepfunc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RonenNess%2Fgrepfunc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RonenNess","download_url":"https://codeload.github.com/RonenNess/grepfunc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248083402,"owners_count":21045090,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["python","python-","python-2","python-3","regex","search","string"],"created_at":"2024-09-24T19:29:18.554Z","updated_at":"2025-04-09T17:52:50.326Z","avatar_url":"https://github.com/RonenNess.png","language":"Python","readme":"# GrepFunc\nSimple grep-like function for Python.\n\nSource at [GitHub](https://github.com/RonenNess/grepfunc).\nDocs at [PythonHosted.org](http://pythonhosted.org/grepfunc/).\n\n## Install\n\nInstall GrepFunc via pip:\n\n```python\npip install grepfunc\n```\n\n## How to use\n\n```GrepFunc``` provide a single function, ```grep```, which imitates Unix's ```grep``` functionality, but operate on lists and variables instead of files.\n\nThe function accept a single string, an iterable, or an opened file handler to search. The default return value is a list of matching strings from input.\n\n### Flags\n\n```GrepFunc``` comes with a set of flags you can set to change ```grep```'s behavior (implemented as named arguments).\n\nThe flags try to imitate the Unix ```grep``` flags as much as possible, but obviously since we are dealing with variables and not files, some flags were omitted / changed.\n\nThe following flags are currently supported:\n\n```\n    # unix grep imitating flags:\n    - F, fixed_strings:      Interpret 'pattern' as a string or a list of strings, any of which is to be matched.\n                             If not set, will interpret 'pattern' as a python regular expression.\n    - i, ignore_case:        Ignore case.\n    - v, invert:             Invert (eg return non-matching lines / values).\n    - w, words:              Select only those lines containing matches that form whole words.\n    - x, line:               Select only matches that exactly match the whole line.\n    - c, count:              Instead of the normal output, print a count of matching lines.\n    - m NUM, max_count:      Stop reading after NUM matching values.\n    - A NUM, after_context:  Return NUM lines of trailing context after matching lines. This will replace the string\n                             part of the reply to a list of strings. Note that in some input types this might skip\n                             following matches. For example, if the input is a file or a custom iterator.\n    - B NUM, before_context: Return NUM lines of leading context before matching lines. This will replace the string\n                             part of the reply to a list of strings.\n    - q, quiet:              Instead of returning string / list of strings return just a single True / False if\n                             found matches.\n    - b, byte_offset:        Instead of a list of strings will return a list of (offset, string), where offset is\n                             the offset of the matched 'pattern' in line.\n    - n, line_number:        Instead of a list of strings will return a list of (index, string), where index is the\n                             line number.\n    - o, only_matching:      Return only the part of a matching line that matches 'pattern'.\n\n    # non standard flags\n    - r, regex_flags:        Any additional regex flags you want to add when using regex (see python re flags).\n    - k, keep_eol            When iterating file, if this option is set will keep the end-of-line at the end of every\n                             line. If not (default) will trim the end of line character.\n    - t, trim                Trim all whitespace characters from every line processed.\n\n```\n\n### Usage examples\n\nThe following is a set of examples to show how to use ```GrepFunc```.\n\n#### Simple grep\n\nThe following example will return a list of movie names with the word 'dog' in them.\n\n```python\nfrom grepfunc import grep\n\nmovies = ['Ghost Dog', 'Die Hard', 'Matrix', 'The Ring', 'Cats and Dogs', 'Batman', 'Superman', 'Reservoir Dogs']\n\n# grep titles with the word 'dog' in them. Note: i=True will ignore case.\ngrep(movies, \"dog\", i=True)\n\n# output:\n# \u003e\u003e\u003e ['Ghost Dog', 'Cats and Dogs', 'Reservoir Dogs']\n```\n\n#### Invert grep\n\nThe following example will return a list of movie names without the word 'dog' in them.\n\n```python\nfrom grepfunc import grep\n\nmovies = ['Ghost Dog', 'Die Hard', 'Matrix', 'The Ring', 'Cats and Dogs', 'Batman', 'Superman', 'Reservoir Dogs']\n\n# grep titles *without* the word 'dog' in them. Note: i=True will ignore case.\ngrep(movies, \"dog\", i=True, v=True)\n\n# output:\n# \u003e\u003e\u003e ['Die Hard', 'Matrix', 'The Ring', 'Batman', 'Superman']\n```\n\n#### Grep on file\n\nThe following example will return a list of movie names without the word 'dog' in them, read from a file.\n\n```python\nfrom grepfunc import grep\n\n# the file contains the following list of words: ['Ghost Dog', 'Die Hard', 'Matrix', 'The Ring', 'Cats and Dogs', 'Batman', 'Superman', 'Reservoir Dogs']\ninfile = open('movies.txt', 'r')\n\n# grep titles *without* the word 'dog' in them from file. Note: i=True will ignore case.\ngrep(infile, \"dog\", i=True)\n\n# output:\n# \u003e\u003e\u003e ['Ghost Dog', 'Cats and Dogs', 'Reservoir Dogs']\n```\n\n#### Quiet grep\n\nThe following example Will return True if the list contains a movie title with the word 'dog'.\n\n```python\nfrom grepfunc import grep\n\nmovies = ['Ghost Dog', 'Die Hard', 'Matrix', 'The Ring', 'Cats and Dogs', 'Batman', 'Superman', 'Reservoir Dogs']\n\n# grep if the word 'dog' appears in any of the movie titles. Note: set i=True to ignore case.\ngrep(movies, \"dog\", i=True, q=True)\n\n# output:\n# \u003e\u003e\u003e True\n```\n\n### Iterator\n\nSometimes you need to scan a really large file, or an endless source of input (like an open socket etc). To handle those cases, you can use the alternative ```grep_iter``` function:\n\n```python\nfrom grepfunc import grep_iter\n\n# a really large file that contain the names of all people in the world\ninfile = open('all_people_in_the_world.txt', 'r')\n\n# print all people with first name 'Bob'\nfor person in grep_iter(infile, \"^Bob\\s\"):\n    print person\n```\n\n```grep_iter``` works just like ```grep```, but will return a memory efficient iterator instead of a list.\n\n## Run Tests\n\nFrom ```GrepFunc``` root dir:\n\n```shell\ncd tests\npython test_all.py\n```\n\nNote that the tests are not included in the pypi package, so to run them please clone the git repository from [GitHub](https://github.com/RonenNess/grepfunc).\n\n## Changes\n\n### 1.0.3\n\n- Removed 'raise StopIteration' to support Python \u003e 3.5.\n\n### 1.0.2\n\n- Fixed imports for Python 3.\n\n## Update Package\n\nAfter updating config files and version:\n\n```\npy -2 setup.py sdist\ntwine upload dist/grepfunc-\u003cnew-version\u003e.zip\n```\n\n## Contact\n\nFor bugs please use [GitHub issues](https://github.com/RonenNess/grepfunc/issues).\nFor other matters feel free to contact me at ronenness@gmail.com.\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fronenness%2Fgrepfunc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fronenness%2Fgrepfunc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fronenness%2Fgrepfunc/lists"}