Environment variable helpers
Project description
SwePin
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
pin4 = SwePinStrict("8012241234", PinFormat.SHORT_WITHOUT_SEPARATOR) # 10 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) # "801224-1234" (10 digits with separator)
print(pin.short_str_repr_w_separator) # "8012241234" (10 digits, no 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.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file swepin-1.1.2.tar.gz.
File metadata
- Download URL: swepin-1.1.2.tar.gz
- Upload date:
- Size: 18.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9955834a68cf3a75fd1e66fc490907ac2ebd72406e0f48bea5a4cb1fcace2af2
|
|
| MD5 |
005b1192e449901b81e8ee608d2ea55c
|
|
| BLAKE2b-256 |
51c5d87ab70cf2d8409180be87101a7ad49daff4442e68d7b4f038b4e73247eb
|
File details
Details for the file swepin-1.1.2-py3-none-any.whl.
File metadata
- Download URL: swepin-1.1.2-py3-none-any.whl
- Upload date:
- Size: 14.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
745b7dd347dcb91ea24e5ce9e46d27b5fe9ba71d1654e76d5166d80d5979ab5a
|
|
| MD5 |
159ff5961e5b6694d7e13d009803f5d8
|
|
| BLAKE2b-256 |
a1bafbd8e0d893893cae4917211d7f5d01270c81d8ffbafcad25f6404bbad894
|