{"id":20548556,"url":"https://github.com/kenkundert/rkm_codes","last_synced_at":"2025-10-03T17:32:03.377Z","repository":{"id":146202636,"uuid":"148403719","full_name":"KenKundert/rkm_codes","owner":"KenKundert","description":"Number format for compact robust labels","archived":false,"fork":false,"pushed_at":"2024-11-16T17:00:24.000Z","size":92,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-20T13:18:32.432Z","etag":null,"topics":["quantiphy","rkm-codes"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/KenKundert.png","metadata":{"files":{"readme":"README.rst","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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2018-09-12T01:36:04.000Z","updated_at":"2024-11-16T17:00:28.000Z","dependencies_parsed_at":null,"dependency_job_id":"bc6db98a-185e-40a2-8042-bd605e08bce0","html_url":"https://github.com/KenKundert/rkm_codes","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/KenKundert/rkm_codes","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KenKundert%2Frkm_codes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KenKundert%2Frkm_codes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KenKundert%2Frkm_codes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KenKundert%2Frkm_codes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/KenKundert","download_url":"https://codeload.github.com/KenKundert/rkm_codes/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KenKundert%2Frkm_codes/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264816474,"owners_count":23668201,"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":["quantiphy","rkm-codes"],"created_at":"2024-11-16T02:13:51.811Z","updated_at":"2025-10-03T17:32:03.371Z","avatar_url":"https://github.com/KenKundert.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":".. initialize RKM codes\n\n    \u003e\u003e\u003e from rkm_codes import set_prefs\n    \u003e\u003e\u003e set_prefs(\n    ...     rkm_maps=None, units_to_rkm_base_code=None, map_sf=None,\n    ...     show_units=None, strip_zeros=None, minus_sign=None, prec=None\n    ... )\n\nRKM codes\n=========\n\n| Version: 1.0\n| Released: 2025-09-14\n|\n\n.. image:: https://pepy.tech/badge/rkm_codes/month\n    :target: https://pepy.tech/project/rkm_codes\n\n..  image:: https://github.com/KenKundert/rkm_codes/actions/workflows/build.yaml/badge.svg\n    :target: https://github.com/KenKundert/rkm_codes/actions/workflows/build.yaml\n\n.. image:: https://img.shields.io/coveralls/KenKundert/rkm_codes.svg\n    :target: https://coveralls.io/r/KenKundert/rkm_codes\n\n.. image:: https://img.shields.io/pypi/v/rkm_codes.svg\n    :target: https://pypi.python.org/pypi/rkm_codes\n\n.. image:: https://img.shields.io/pypi/pyversions/rkm_codes.svg\n    :target: https://pypi.python.org/pypi/rkm_codes/\n\nRKM codes are used to represent electrical quantities in labels, particularly on\nschematics and on the components themselves.  They are standardized in various\nnational and international standards, including: IEC 60062 (1952) (formerly IEC 62),\nDIN 40825 (1973), BS 1852 (1974), IS 8186 (1976) and EN 60062 (1993).\nIEC-60062 was significantly updated in 2016.\n\nRKM codes were originally used as part marking codes.  This shorthand\nnotation is widely used in electrical engineering to denote the values of\nresistors and capacitors in circuit diagrams and in the production of electronic\ncircuits (for example in bills of material and in silk screens). This method\navoids overlooking the decimal separator, which may not be rendered reliably on\ncomponents or when duplicating documents.\n\nThe popularity of RKM codes was fading because they address a problem that is \nless common today. However they are making something of a come back as all the \ncharacters in a RKM code are either letters or digits and so they can be \nembedded in a software identifier without introducing illegal characters.\n\nIEC 60062 is described in https://en.wikipedia.org/wiki/RKM_code.\n\nEssentially an RKM version of a number is the number with a scale factor where\nthe decimal point is replaced by the scale factor. For example, a resistance of\n4.7kΩ becomes 4k7. If there is no scale factor, the decimal point is replaced by\na letter that signifies the type of the component.  For example, a resistance of\n4.7Ω becomes 4r7.\n\nResistance examples::\n\n    R47 → 0.47 Ω\n    4R7 → 4.7 Ω\n    470R → 470 Ω\n    4K7 → 4.7 kΩ\n    47K → 47 kΩ\n    47K3 → 47.3 kΩ\n    470K → 470 kΩ\n    4M7 → 4.7 MΩ\n\nIn the standard, large values are assumed to be resistances and small values are\nassumed to be capacitances.  So 4k7 is a resistance and 2n5 is a capacitance.\nHowever, this package also supports a version of RKM codes where the units are\nnot implied by the value, making RKM codes suitable for a wider variety of value\ntypes, such as voltage, current, and inductance.\n\n\nInstalling\n----------\n\nThis package converts RKM codes to `QuantiPhy Quantities\n\u003chttps://quantiphy.readthedocs.io\u003e`_ and Quantities to RKM codes.\n\nInstall with::\n\n    pip3 install --user rkm_codes\n\nRequires Python 3.6 or better.\n\n\nConverting to and from RKM Codes\n--------------------------------\n\nThe following is a simple example of how to convert back and forth between RKM\ncodes and Quantities::\n\n    \u003e\u003e\u003e from rkm_codes import from_rkm, to_rkm\n    \u003e\u003e\u003e r = from_rkm('6K8')\n    \u003e\u003e\u003e r\n    Quantity('6.8k')\n\n    \u003e\u003e\u003e to_rkm(r)\n    '6K8'\n\nNotice that in this case the quantity does not include units. That is because by\ndefault *rkm_codes* assumes unitless numbers. You can change this behavior.  Out\nof the box *rkm_codes* supports two kinds of numbers, unitless and those that\nfollow the IEC60062 standard. You can switch between those two kinds of numbers\nusing something like this::\n\n    \u003e\u003e\u003e from rkm_codes import set_prefs, IEC60062_MAPS, UNITLESS_MAPS\n    \u003e\u003e\u003e r = from_rkm('6k8')\n    \u003e\u003e\u003e r\n    Quantity('6.8k')\n\n    \u003e\u003e\u003e set_prefs(rkm_maps=IEC60062_MAPS)\n    \u003e\u003e\u003e from_rkm('6k8')\n    Quantity('6.8 kΩ')\n\n    \u003e\u003e\u003e set_prefs(rkm_maps=UNITLESS_MAPS)\n    \u003e\u003e\u003e from_rkm('6k8')\n    Quantity('6.8k')\n\nIn either case, *rkm_codes* allows you to explicitly specify the units, which\nalways overrides any implied units::\n\n    \u003e\u003e\u003e set_prefs(rkm_maps=UNITLESS_MAPS)\n    \u003e\u003e\u003e from_rkm('6kΩ8')\n    Quantity('6.8 kΩ')\n\n    \u003e\u003e\u003e i = from_rkm('2uA5')\n    \u003e\u003e\u003e i\n    Quantity('2.5 uA')\n\nThe primary argument for *to_rkm* can be a string, a float, or a quantity::\n\n    \u003e\u003e\u003e print(to_rkm('12.5 nA', prec=2))\n    12n5\n\n    \u003e\u003e\u003e print(to_rkm(12.5e-9, prec=2))\n    12n5\n\n    \u003e\u003e\u003e from quantiphy import Quantity\n    \u003e\u003e\u003e print(to_rkm(Quantity('12.5 nA'), prec=2))\n    12n5\n\nWhen converting to an RKM code, you can instruct that the units be included::\n\n    \u003e\u003e\u003e to_rkm(i, show_units=True)\n    '2µA5'\n\nYou can also indicate how many digits should be included::\n\n    \u003e\u003e\u003e to_rkm(i.add(1e-9), prec=5, show_units=True)\n    '2µA501'\n\nNormally, any excess zeros are removed, but you can change that too::\n\n    \u003e\u003e\u003e to_rkm(i.add(1e-9), prec=5, show_units=True, strip_zeros=False)\n    '2µA50100'\n\nTo shorten the output code it is possible to remove the base code when it is\nextraneous::\n\n    \u003e\u003e\u003e from quantiphy import Quantity\n    \u003e\u003e\u003e to_rkm(Quantity('470Ω'), show_units=False)\n    '470'\n\n    \u003e\u003e\u003e to_rkm(Quantity('470Ω'), show_units=False, strip_code=False)\n    '470r'\n\nHere is a short program that illustrates some of the options of *to_rkm*::\n\n    \u003e\u003e\u003e from rkm_codes import from_rkm, to_rkm, set_prefs, IEC60062_MAPS\n\n    \u003e\u003e\u003e set_prefs(prec=4)\n\n    \u003e\u003e\u003e q = from_rkm('0μΩ47')\n    \u003e\u003e\u003e while q \u003c 1e6:\n    ...     vals = [\n    ...         q,\n    ...         to_rkm(q),\n    ...         to_rkm(q, strip_code=False),\n    ...         to_rkm(q, show_units=True),\n    ...         to_rkm(q, strip_zeros=False)\n    ...     ]\n    ...     print(' '.join(['  {:\u003c9}'.format(v) for v in vals]).strip())\n    ...     q = q.scale(10)\n    470 nΩ      470n        470n        470nΩ       470n00\n    4.7 uΩ      4µ7         4µ7         4µΩ7        4µ7000\n    47 uΩ       47µ         47µ         47µΩ        47µ000\n    470 uΩ      470µ        470µ        470µΩ       470µ00\n    4.7 mΩ      4m7         4m7         4mΩ7        4m7000\n    47 mΩ       47m         47m         47mΩ        47m000\n    470 mΩ      470m        470m        470mΩ       470m00\n    4.7 Ω       4r7         4r7         4Ω7         4r7000\n    47 Ω        47          47r         47Ω         47r000\n    470 Ω       470         470r        470Ω        470r00\n    4.7 kΩ      4K7         4K7         4KΩ7        4K7000\n    47 kΩ       47K         47K         47KΩ        47K000\n    470 kΩ      470K        470K        470KΩ       470K00\n\nIf you prefer not to use the small SI scale factors, which would be more in\nkeeping with IEC60062 for resistors, you can specify that ``quantiphy.Quantity``\nuse a restricted ``output_sf``::\n\n    \u003e\u003e\u003e q = from_rkm('0μΩ47')\n    \u003e\u003e\u003e q.output_sf = 'TGMk'   # this line is new\n    \u003e\u003e\u003e while q \u003c 1e6:\n    ...     vals = [\n    ...         q,\n    ...         to_rkm(q),\n    ...         to_rkm(q, strip_code=False),\n    ...         to_rkm(q, show_units=True),\n    ...         to_rkm(q, strip_zeros=False)\n    ...     ]\n    ...     print(' '.join(['  {:\u003c9}'.format(v) for v in vals]).strip())\n    ...     q = q.scale(10)\n    470e-9 Ω    0           0r          0Ω          r0000\n    4.7e-6 Ω    0           0r          0Ω          r0000\n    47e-6 Ω     0           0r          0Ω          r0000\n    470e-6 Ω    r0005       r0005       Ω0005       r0005\n    4.7e-3 Ω    r0047       r0047       Ω0047       r0047\n    47e-3 Ω     r047        r047        Ω047        r0470\n    470e-3 Ω    r47         r47         Ω47         r4700\n    4.7 Ω       4r7         4r7         4Ω7         4r7000\n    47 Ω        47          47r         47Ω         47r000\n    470 Ω       470         470r        470Ω        470r00\n    4.7 kΩ      4K7         4K7         4KΩ7        4K7000\n    47 kΩ       47K         47K         47KΩ        47K000\n    470 kΩ      470K        470K        470KΩ       470K00\n\nYou can create your own maps by passing in a dictionary that maps a RKM base\ncode character into a scale factor and units. For example, you could create\na map that uses 'd' or 'D' to represent the decimal point in numbers without\nscale factors rather than 'r', 'c', etc.  For example::\n\n    \u003e\u003e\u003e set_prefs(rkm_maps=dict(d=('', ''), D=('', '')))\n    \u003e\u003e\u003e from_rkm('6d8')\n    Quantity('6.8')\n\n    \u003e\u003e\u003e from_rkm('2d5')\n    Quantity('2.5')\n\nPassing *None* for the value of a map returns it to its default value.\n\nIf *rkm_codes* encounters a RKM base code character that is not in the map, it\nsimply uses that character. In this way, scale factors are handled::\n\n    \u003e\u003e\u003e from_rkm('6k8')\n    Quantity('6.8k')\n\nWhen converting from Quantities to RKM codes, you can override the default\nmappings from units to RKM base code characters. The default mapping maps 'Ω'\nand 'Ohm' to 'r', 'F' to 'c', 'H' to 'l', 'V' to 'v', and 'A' to 'i'.  However,\nyou may prefer uppercase base characters, which is more in alignment with the\noriginal standard. To get that, you can use something like this::\n\n    \u003e\u003e\u003e rkm_base_code_mappings = {\n    ...     'Ω': 'R',\n    ...     'Ohm': 'R',\n    ...     'F': 'C',\n    ...     'H': 'L',\n    ...     'V': 'V',\n    ...     'A': 'I',\n    ... }\n    \u003e\u003e\u003e set_prefs(rkm_maps=IEC60062_MAPS, units_to_rkm_base_code=rkm_base_code_mappings)\n    \u003e\u003e\u003e r = from_rkm('k0012')\n    \u003e\u003e\u003e to_rkm(r)\n    '1R2'\n\nYou can control the scale factors used by to_rkm() by setting *map_sf* using\n*set_prefs*. The default maps 'u' to 'μ' and 'k' to 'K'. You might wish to\nprevent the use of 'μ' while retaining the use of 'K', which you can do with::\n\n    \u003e\u003e\u003e set_prefs(map_sf=dict(u='µ'))\n    \u003e\u003e\u003e c = from_rkm('5u')\n    \u003e\u003e\u003e to_rkm(c)\n    '5µ'\n\n\nFinding RKM Codes\n-----------------\n\n*find_rkm* is available for finding the RKM codes embedded in text strings.\nUsing it, you can iterate through all the numbers specified using RKM::\n\n    \u003e\u003e\u003e from rkm_codes import find_rkm\n\n    \u003e\u003e\u003e text = '''\n    ...     An RKM code that may include explicitly specified. Examples of\n    ...     acceptable RKM codes for resistance include:   R47 (0.47 Ω), 4R7\n    ...     (4.7 Ω), 470R (470 Ω), 4K7 (4.7 kΩ), 47K (47 kΩ), 47K3 (47.3 kΩ),\n    ...     470K (470 kΩ), and 4M7 (4.7 MΩ).\n    ... '''\n    \u003e\u003e\u003e for num in find_rkm(text):\n    ...     print(num)\n    470 mΩ\n    4.7 Ω\n    470 Ω\n    4.7 kΩ\n    47 kΩ\n    47.3 kΩ\n    470 kΩ\n    4.7 MΩ\n\nWhen the RKM code is not isolated by punctuation or spaces it can get confused\nby leading and trailing text.  You can often resolve this issue by restricting\nthe matches to either the leading or trailing digit forms of the RKM code. Do so\nby specifying either 'ld' or 'td' as a second argument.  For example::\n\n    \u003e\u003e\u003e for num in find_rkm('sink200nA'):\n    ...     print(num)\n    200 msink\n\n    \u003e\u003e\u003e for num in find_rkm('sink200nA', 'ld'):\n    ...     print(num)\n    200 nA\n\n\nPin Name Generator Example\n--------------------------\n\nAs a practical example of the use of RKM codes, imagine wanting a program that\ncreates pin names for an electrical circuit based on a naming convention.  It\nwould take a table of pin characteristics that are used to create the names.\nFor example::\n\n    \u003e\u003e\u003e from quantiphy import Quantity\n    \u003e\u003e\u003e from rkm_codes import to_rkm, set_prefs as set_rkm_prefs\n\n    \u003e\u003e\u003e pins = [\n    ...     dict(kind='ibias', direction='out', polarity='sink', dest='dac', value='250nA'),\n    ...     dict(kind='ibias', direction='out', polarity='src', dest='rampgen', value='2.5μA'),\n    ...     dict(kind='vref', direction='out', dest='dac', value='1.25V'),\n    ...     dict(kind='vdda', direction='in', value='2.5V'),\n    ... ]\n    \u003e\u003e\u003e set_rkm_prefs(map_sf={}, units_to_rkm_base_code=None, show_units=True, prec=2)\n\n    \u003e\u003e\u003e for pin in pins:\n    ...     components = []\n    ...     if 'value' in pin:\n    ...         pin['VALUE'] = to_rkm(Quantity(pin['value']))\n    ...     for name in ['dest', 'kind', 'direction', 'VALUE', 'polarity']:\n    ...         if name in pin:\n    ...             components.append(pin[name])\n    ...     print('_'.join(components))\n    dac_ibias_out_250nA_sink\n    rampgen_ibias_out_2uA5_src\n    dac_vref_out_1V25\n    vdda_in_2V5\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkenkundert%2Frkm_codes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkenkundert%2Frkm_codes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkenkundert%2Frkm_codes/lists"}