{"id":13601741,"url":"https://github.com/slongfield/pyfmt","last_synced_at":"2025-04-11T04:31:26.973Z","repository":{"id":28166619,"uuid":"105104916","full_name":"slongfield/pyfmt","owner":"slongfield","description":"Golang implementation of PEP3101","archived":false,"fork":false,"pushed_at":"2022-02-22T01:26:20.000Z","size":100,"stargazers_count":73,"open_issues_count":0,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-08-02T18:41:12.204Z","etag":null,"topics":["golang","golang-library","golang-package","python-golang-translation","string-formatter"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/slongfield.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-09-28T05:13:48.000Z","updated_at":"2024-05-26T10:21:22.000Z","dependencies_parsed_at":"2022-08-07T13:15:25.519Z","dependency_job_id":null,"html_url":"https://github.com/slongfield/pyfmt","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slongfield%2Fpyfmt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slongfield%2Fpyfmt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slongfield%2Fpyfmt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slongfield%2Fpyfmt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/slongfield","download_url":"https://codeload.github.com/slongfield/pyfmt/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223458558,"owners_count":17148483,"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":["golang","golang-library","golang-package","python-golang-translation","string-formatter"],"created_at":"2024-08-01T18:01:07.330Z","updated_at":"2024-11-07T04:31:34.424Z","avatar_url":"https://github.com/slongfield.png","language":"Go","readme":"# pyfmt\n![Build Status](https://github.com/slongfield/pyfmt/actions/workflows/go.yml/badge.svg)\n\npyfmt implements Python's advanced string formatting in Golang.\n\nThis is an alternative to the `fmt` package's Sprintf-style string formatting and mimics the\n.format() style formatting available in Python \u003e2.6. Under the hood, it still uses the go 'fmt'\nlibrary, for better compatibility with Go types at the expense of not being as exact of a clone of\nPython format strings.\n\nBraces {} are used to indicate 'format items', anything outside of braces will be emitted directly\ninto the output string, and anything inside will be used to get values from the other function\narguments and format them. The one exception is double braces, '{{' and '}}', which will cause\nliteral '{' and '}' runes to be emitted in the output.\n\nEach format item consists of a 'field name', which indicates which value from the argument list to\nuse, and a 'format specifier', which indicates how to format that item.\n\nRequires Golang 1.5 or newer for formatting, 1.9 or newer to run the tests.\n\n# Functions\n\npyfmt implements three functions, 'Fmt', 'Must', and 'Error'. 'Fmt' formats, but may return an error\nas detailed below. 'Must' formats, but will panic when 'Fmt' would return an error, and 'Error' acts\nlike 'Fmt', but returns an error type. In the event that there's an error formatting the error,\n'Error' includes the format error and as much of the formatted string as possible.\n\nAll of them take a format string and arguments to be used in formatting that string.\n\n# Getting Values from Field Names\n\nValues can be fetched from field names in two forms: simple names or compound names. All compound\nnames build off of simple names.\n\n## Simple field names:\n\nThe simplest look up treats the argument list as just a list. There are two possible ways to look up\nelements from this list. First, by {}, which gets the 'next' item and by {n}, which gets the nth\nitem. Accessing these two ways is independent but cannot be mixed, and\n\n```\n  pyfmt.Must(\"{} {} {}\", ...)\n```\n\nis equivalent to\n\n```\n  pyfmt.Must(\"{0} {1} {2}\", ...)\n```\n\nAccessing an element that's outside the list range will return an error (with Fmt) or panic (with\nMust).\n\nThe first element in the list is treated specially if it's a struct or a map with string keys,\nallowing the elements from that struct or map can be directly accessed. For instance:\n\n```\n  pyfmt.Must(\"{test}\", map[string]int{\"test\": 5}) --\u003e \"5\"\n```\n\nsimilarly for structs:\n\n```\n  pyfmt.Must(\"{test}\": myStruct{test: 5}) --\u003e \"5\"\n```\n\nAttempting to read from an undefined key will return an error or panic, depending on if it was\naccessed with Fmt or Must.\n\n## Compound field names:\n\nIf the value referenced by the field is itself a List, map[string]interface{}, or struct, it can be\nfurther accessed in the format string.\n\nLists are accessed with square brackets:\n\n```\n  pyfmt.Must(\"{0[0]}\", []string{\"test\"}) --\u003e \"test\"\n```\n\nsimilarly, maps are accessed with square brackets:\n\n```\n  pyfmt.Must(\"{0[test]}\", map[string]interface{}{\"test\": \"42\"}) --\u003e \"42\"\n```\n\nand struct fields are accessed with a period ('.')\n\n```\n  pyfmt.Must(\"{foo.bar.baz}\", MyStruct{foo: Foo{bar: Bar{baz: \"test\"}}}) --\u003e \"test\"\n```\n\n# Formatting\n\nIf after a simple or complex field name, there's a ':', what follows is considered to be the format\nspecifier. If a type satisfies the PyFormat interface (discussed below), the format specifier will\nbe passed to that, but otherwise, it will fall back to the default formatter, which expects the\nstandard format specifier:\n\n```\n  [[fill]align][sign][#][0][minimumwidth][.precision][type]\n```\n\nThe optional align feature can be one of the following:\n\n```\n  '\u003c': left-aligned\n  '\u003e': right-aligned (this is the default)\n  '=': padding after the sign, but before the digits (e.g., for +000042)\n  '^': centered\n```\n\nIf an align flag is defined, a 'fill' character can also be defined. If undefined, space (' ') will\nbe used.\n\nThe optional 'sign' is only valid for numeric types and can be:\n\n```\n  '+': Show sign for both positive and negative numbers\n  '-': Show sign only for negative numbers (default)\n  ' ': use a leading space for positive numbers\n```\n\nIf # is present, when using the binary, octal, or hex types, a '0b', '0o', or '0x' will be\nprepended, respectively.\n\nThe minimumwidth field specifies a minimum width, which is helpful when used with alignment. If\npreceded with a zero, numbers will be zero-padded.\n\nThe precision field specifies a maximum width for non-floating point, non-integer types, and the\nnumber of points to show after the decimal point for floating types.\n\nThe 'type' format determines what type the value will be formatted as.\n\nFor integers:\n\n```\n  'b' - Binary, base 2\n  'd' - Decimal, base 10 (default)\n  'o' - Octal, base 8\n  'x' - Hexadecimal, base 16\n  'X' - Hexadecimal, base 16, using upper-case letters\n```\n\nFor floats and complex numbers:\n\n```\n  'e' - Scientific notation\n  'E' - Similar to e, but uppercase\n  'f' - Fixed point, displays the number as a fixed-point number.\n  'F' - Same, but uppercase.\n  'g' - General format, prints as a fixed point unless it's too large, then switches to scientific\n        notation. (default)\n  'G' - Similar to g, but uses capital letters\n  '%' - Percentage, multiplies the number by 100 and displays it with a '%' sign. Can also be\n        applied to integer types.\n```\n\n## Special Formatting Types\n\nFor some types (most notably structs), the default formatter doesn't quite give enough information\nto understand the value after its printed, so it's useful to get more accurate Go representations.\nAdditionally, sometimes it's useful to print the type of a variable while formatting it. For these,\npyfmt allows for some special formatting types that aren't in the Python format syntax.\n\n```\n  'r' - convert the value to its Go-syntax representation\n  't' - convert the value to its Go type\n  's' - if printing a struct, print the struct field names\n```\n\nThese are equivalent to the `%#v`, `%T` and `%+v` format strings in the \"fmt\" package, but don't\nhave an exact equivalent in Python.\n\n# Custom formatters\n\nInternally, pyfmt uses Go's fmt package, so existing types satisfying its Formatter, GoStringer,\nor Stringer interfaces will use those implementations as appropriate.\n\nIf the type satisfies the PyFormatter interface, the format specifier will be passed to that\nfunction, for custom formatting. Due to the limits of Golang reflection, if accessing a struct\nsub-field that has a custom formatter, the struct field must be exported for pyfmt to access the\ncustom Formatter. This is similar to the default 'fmt' package, which doesn't apply custom Stringer\nimplementations to unexported struct fields.\n\n# TODOs\n\n  *  Improve performance. Some of the string manipulations allocate more frequency than they need\n     to, causing slowdown relative to the built-in 'fmt' library.\n","funding_links":[],"categories":["Go"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fslongfield%2Fpyfmt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fslongfield%2Fpyfmt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fslongfield%2Fpyfmt/lists"}