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
  • ๐Ÿ”’ Strict mode for enforcing exact format requirements

Installation

pip install swepin

Quick Start

# Import using the full name
from swepin import SwedishIdentityPersonalNumber

# Or using the shorter alias
from swepin import SwePin

# For strict format validation (YYYYMMDD-NNNN only)
from swepin import SwePinStrict

# 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 a.k.a samordningsnummer)
  โ”‚   โ”‚   โ”‚
  โ”‚   โ”‚   โ””โ”€โ”€ 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 with SwePin
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)

Strict Format Validation

For applications that require exact format compliance, use SwePinStrict which enforces specific PIN formats through the PinFormat enum:

from swepin import SwePinStrict, PinFormat

# Default format (LONG_WITH_SEPARATOR)
pin = SwePinStrict("19801224-1234")  # โœ… Uses default format
pin = SwePinStrict("19801284-1234")  # โœ… Valid coordination number

# Specify format explicitly
pin1 = SwePinStrict("19801224-1234", PinFormat.LONG_WITH_SEPARATOR)    # 13 chars
pin2 = SwePinStrict("198012241234", PinFormat.LONG_WITHOUT_SEPARATOR)  # 12 chars
pin3 = SwePinStrict("801224-1234", PinFormat.SHORT_WITH_SEPARATOR)     # 11 chars

### Format Conversion

Easily convert between different representations:

```python
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_w_separator)   # "8012241234" (10 digits with separator)

Generate Random Valid PINs for Testing

Read this!

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/validation 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.1.6.tar.gz (18.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.1.6-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: swepin-1.1.6.tar.gz
  • Upload date:
  • Size: 18.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for swepin-1.1.6.tar.gz
Algorithm Hash digest
SHA256 e832ca3c3028de16cc4e01af98f17557cab6c3989e57b565040c06e63f4c8dbe
MD5 6e382b9a8a90f64d787ee1f9f0d5295d
BLAKE2b-256 bbd3e66ddf964b96b6e15a6f5aa5c3545c84c2b9b47a52d35aa9c4f227d53f5b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: swepin-1.1.6-py3-none-any.whl
  • Upload date:
  • Size: 14.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for swepin-1.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 15e27ec7b0fc860546c29d952062c6d840901f90ae5988f3e6d2d0f4a2eb5fbc
MD5 a6e36d50dd679607aea69f62c348439d
BLAKE2b-256 f995fbc60cc2cdd11358dd63bc1ec07dc0445a8ce59dda95d507e60ed4eda0d5

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