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.3.tar.gz (18.1 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.3-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for swepin-1.1.3.tar.gz
Algorithm Hash digest
SHA256 59b4b2452ec54f7fe207113017060f7564f0e7cd15d22e645425c74a005b12d9
MD5 19cd242e0d123486140cec057f697c1f
BLAKE2b-256 3a3da62892c57d62bda789e2857e754a3acb5afdc73bef8a9983aae1429fea2e

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for swepin-1.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 cdd256e94c8f19a8a579b22713a78177976149d062e523d2f7f795f390c8df40
MD5 a1bb694c4622ed3dfca0d1eb956507f5
BLAKE2b-256 be9b9231a8f814532419b2af158ab06033453098200b2c15eebf5206288c918b

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