Skip to main content

Environment variable helpers

Project description

SwePin

PyPI version Python versions License Documentation

A comprehensive library for parsing, validating, and handling Swedish Personal Identity Numbers (personnummer)

Features

  • โœ… Validate Swedish Personal Identity Numbers
  • ๐Ÿ“Š Parse and extract all components (birth date, gender, validation digit, etc.)
  • ๐ŸŒ Multi-language support (English and Swedish)
  • ๐Ÿงฎ Age calculation with customizable reference date
  • ๐Ÿ”„ Format conversion (with/without separators, 10/12 digits)
  • โš™๏ธ Support for coordination numbers and centenarians
  • ๐ŸŽฒ Generate valid random PIN numbers for testing

Installation

pip install swepin

Quick Start

# Import using the full name
from swepin import SwedishIdentityPersonalNumber

# Or using the shorter alias
from swepin import SwePin

# Parse a Swedish Personal Identity Number
pin = SwePin("198012241234")

# Get basic information
print(f"Birth date: {pin.get_date()}")          # 1980-12-24
print(f"Age: {pin.age}")                        # Current age based on today's date
print(f"Gender: {'Male' if pin.male else 'Female'}")

# Display detailed information
print(pin.pretty_print())                       # Prints a formatted table with all details

# Get structured data
pin.dict                                        # Dictionary representation
pin.json                                        # JSON representation

Understanding Swedish Personal Identity Numbers

Swedish Personal Identity Numbers follow this format: YYYYMMDD-XXXX or YYMMDD-XXXX (or + instead of - for separator)

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  BIRTH DATE   โ”‚ SEP   โ”‚   BIRTH NUMBER    โ”‚
โ”œโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ C โ”‚ Y โ”‚ M โ”‚ D โ”‚ - / + โ”‚ B โ”‚ B โ”‚ G โ”‚ Valid โ”‚
โ””โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  โ”‚   โ”‚   โ”‚   โ”‚     โ”‚     โ”‚   โ”‚   โ”‚     โ”‚
  โ”‚   โ”‚   โ”‚   โ”‚     โ”‚     โ”‚   โ”‚   โ”‚     โ””โ”€โ”€ Validation Digit (Luhn algorithm)
  โ”‚   โ”‚   โ”‚   โ”‚     โ”‚     โ”‚   โ”‚   โ”‚
  โ”‚   โ”‚   โ”‚   โ”‚     โ”‚     โ”‚   โ”‚   โ””โ”€โ”€ Gender Digit (odd = male, even = female)
  โ”‚   โ”‚   โ”‚   โ”‚     โ”‚     โ”‚   โ”‚
  โ”‚   โ”‚   โ”‚   โ”‚     โ”‚     โ””โ”€โ”€โ”€โ”ดโ”€โ”€ Birth Place (regional code for pre-1990)
  โ”‚   โ”‚   โ”‚   โ”‚     โ”‚
  โ”‚   โ”‚   โ”‚   โ”‚     โ””โ”€โ”€ Separator (- if < 100 years old, + if >= 100)
  โ”‚   โ”‚   โ”‚   โ”‚
  โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ Day (01-31, or 61-91 for coordination numbers)
  โ”‚   โ”‚   โ”‚
  โ”‚   โ”‚   โ””โ”€โ”€ Month (01-12)
  โ”‚   โ”‚
  โ”‚   โ””โ”€โ”€ Year (last two digits)
  โ”‚
  โ””โ”€โ”€ Century (optional in short format, derived when not provided)

Features

Multiple Format Support

SwePin supports all standard formats of Swedish Personal Identity Numbers:

# All these are valid and will parse correctly
SwePin("198012241234")    # Full format (12 digits)
SwePin("8012241234")      # Short format (10 digits)
SwePin("19801224-1234")   # With separator
SwePin("801224-1234")     # Short with separator
SwePin("19801284-1234")   # Coordination number (day 24 + 60 = 84)
SwePin("121212+1212")     # Person over 100 years old (+ separator)

Format Conversion

Easily convert between different representations:

pin = SwePin("198012241234")

# Access different format representations
print(pin.long_str_repr)                # "198012241234" (12 digits, no separator)
print(pin.long_str_repr_w_separator)    # "19801224-1234" (12 digits with separator)
print(pin.short_str_repr)               # "801224-1234" (10 digits with separator)
print(pin.short_str_repr_w_separator)   # "8012241234" (10 digits, no separator)

Generate Random Valid PINs for Testing

from swepin.generators import generate_valid_pins
from datetime import date

# Generate 5 random valid PIN objects
pins = generate_valid_pins(5)
for pin in pins:
    print(f"{pin} (Birth Date: {pin.birth_date}, Gender: {'Male' if pin.male else 'Female'})")

# Generate PINs with specific parameters
male_pins = generate_valid_pins(3, male_ratio=1.0)
old_pins = generate_valid_pins(3, start_year=1900, end_year=1923, include_centenarians=True)
coord_pins = generate_valid_pins(3, include_coordination_numbers=True)

# Generate PIN dictionaries or JSON
pin_dicts = generate_valid_pins(5, to_dict=True)
pin_jsons = generate_valid_pins(5, to_json=True)

Language Support

from swepin.swedish_personal_identity_number import SwedishPersonalIdentityNumber, Language

pin = SwedishPersonalIdentityNumber("198012241234")

# Get output in different languages
print(pin.pretty_print(language=Language.ENG))  # Default - English
print(pin.pretty_print(language=Language.SWE))  # Swedish

# Get dictionary with Swedish keys
sv_dict = pin.to_dict(language=Language.SWE)

Detailed Information

Get comprehensive information about a personal number with a beautiful formatted display:

pin = SwePin("198012241234")
print(pin.pretty_print())

Output:

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”“
โ”ƒ  Swedish Personal Identity Number Details                         โ”ƒ
โ”ฃโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‹โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ซ
โ”ƒ         Property         โ”ƒ                 Value                  โ”ƒ
โ”ฃโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‹โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ซ
โ”ƒ      Original Number     โ”ƒ 198012241234                           โ”ƒ
โ”ฃโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‹โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ซ
โ”ƒ        BIRTH DATE        โ”ƒ                                        โ”ƒ
โ”ฃโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‹โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ซ
โ”ƒ         Century          โ”ƒ 19                                     โ”ƒ
โ”ƒ      Year (2 digits)     โ”ƒ 80                                     โ”ƒ
โ”ƒ    Full Year (4 digits)  โ”ƒ 1980                                   โ”ƒ
โ”ƒ          Month           โ”ƒ 12                                     โ”ƒ
โ”ƒ           Day            โ”ƒ 24                                     โ”ƒ
โ”ƒ         Full Date        โ”ƒ 1980-12-24                             โ”ƒ
โ”ƒ    Coordination Number   โ”ƒ No                                     โ”ƒ
โ”ฃโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‹โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ซ
โ”ƒ         SEPARATOR        โ”ƒ -                                      โ”ƒ
โ”ฃโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‹โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ซ
โ”ƒ       BIRTH NUMBER       โ”ƒ                                        โ”ƒ
โ”ฃโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‹โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ซ
โ”ƒ      Complete Number     โ”ƒ 123                                    โ”ƒ
โ”ƒ     Birth Place Digits   โ”ƒ 12                                     โ”ƒ
โ”ƒ       Gender Digit       โ”ƒ 3                                      โ”ƒ
โ”ƒ     Validation Digit     โ”ƒ 4                                      โ”ƒ
โ”ฃโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‹โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ซ
โ”ƒ    DERIVED PROPERTIES    โ”ƒ                                        โ”ƒ
โ”ฃโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‹โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ซ
โ”ƒ           Age            โ”ƒ 44                                     โ”ƒ
โ”ƒ          Gender          โ”ƒ Male                                   โ”ƒ
โ”ฃโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‹โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ซ
โ”ƒ         FORMATS          โ”ƒ                                        โ”ƒ
โ”ฃโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‹โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ซ
โ”ƒ      Long (12 digits)    โ”ƒ 198012241234                           โ”ƒ
โ”ƒ         Long (sep)       โ”ƒ 19801224-1234                          โ”ƒ
โ”ƒ  Short (10 digits) (sep) โ”ƒ 801224-1234                            โ”ƒ
โ”ƒ     Short without (sep)  โ”ƒ 8012241234                             โ”ƒ
โ”—โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ปโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”›

Validation

The library validates personal numbers using the Luhn algorithm to ensure the check digit is correct:

try:
    pin = SwePin("198012241234")  # Valid personal number
    print("Valid personal identity number")
except Exception as e:
    print(f"Invalid: {e}")

try:
    pin = SwePin("198012241235")  # Invalid check digit
    print("Valid personal identity number")
except Exception as e:
    print(f"Invalid: {e}")  # Will print error about validation digit mismatch

Special Cases

Coordination Numbers

For people without a permanent residence in Sweden, coordination numbers (samordningsnummer) are used where the day is increased by 60:

pin = SwePin("198012841234")  # Day 24 + 60 = 84
print(f"Is coordination number: {pin._is_coordination_number()}")  # True
print(f"Birth date: {pin.get_date()}")  # Still returns 1980-12-24

Centenarians

For people 100 years or older, a + separator is used instead of - in the short format:

pin = SwePin("121212+1212")  # Person born in 1912
print(pin.short_str_repr)    # "121212+1212"
print(pin.full_year)         # "1912"

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Made with โค๏ธ in Sweden

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

swepin-1.0.0.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

swepin-1.0.0-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file swepin-1.0.0.tar.gz.

File metadata

  • Download URL: swepin-1.0.0.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for swepin-1.0.0.tar.gz
Algorithm Hash digest
SHA256 ce8260e52a688d8a62358a1bccfd8c79b7ca7beb6e82722c887424edcc100eaf
MD5 9c565d86accb781803be38f33decc5a5
BLAKE2b-256 ce6774bd0e5be8e47c21610b8ef93d344265da58456865f57c63e05fc1496cf1

See more details on using hashes here.

File details

Details for the file swepin-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: swepin-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 12.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for swepin-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e303704bec7f986047f514c6873e1bc51a9740fb556b67ec0bcfd42351470780
MD5 9bdc8f067f3c27274517b5e91e614c50
BLAKE2b-256 25f7e5cb5152060bbffceac72f8c87e53c19a47e445441ec6239609164c94c44

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page