Skip to main content

Swifpy makes Python Swifty. It provides some types which have similar APIs with Swift’s.

from swifpy import Array, Dictionary, Int, Optional, Some, String

array: Array[Int] = Array([2, 3, 5])
squared: Array[Int] = array.map(lambda x: x * x)  # [4, 9, 25]
count: Int = array.count  # 3

dictionary: Dictionary[String, Int] = Dictionary({'a': 2, 'b': 3, 'c': 5})
a: Optional[Int] = dictionary['a']  # Optional(2)
b: Optional[Int] = dictionary['b']  # Optional(3)

sum: Optional[Int] = a.flat_map(lambda x: b.map(lambda y: x + y))  # Optional(5)

Usage

Bool, Float, Int, String

They are just type aliases of bool, float, int and str respectivly.

Array

from swifpy import Array, Int, Optional, Some

numbers: Array[Int] = Array([2, 3, 5])

second: Int = numbers[1]    # 3
count: Int = numbers.count  # 3

squared: Array[Int] = numbers.map(lambda x: x * x)             # [4, 9, 25]
odd: Array[Int] = numbers.filter(lambda x: x % 2 != 0)         # [3, 5]
sum: Array[Int] = numbers.reduce(0, lambda r, x: r + x)        # 10
twice: Array[Int] = numbers.flat_map(lambda x: Array([x, x]))  # [2, 2, 3, 3, 5, 5]

first: Optional[Int] = numbers.first  # Optional(2)
third: Optional[Int] = numbers.last   # Optional(5)

for number in numbers:
    print(number)

Dictionary

from swifpy import Dictionary, Int, Optional, Some, String

dictionary: Dictionary[String, Int] = Dictionary({'a': 2, 'b': 3, 'c': 5})

a: Optional[Int] = dictionary['a']  # Optional(2)
dictionary['d'] = 7
count: Int = dictionary.count  # 4

for key, value in dictionary:
    print("%s -> %d" % (key, value))

Optional

from swifpy import Int, Nil, Optional, Some, NilError

a: Optional[Int] = Some(2)
b: Optional[Int] = Some(3)
n: Optional[Int] = Nil

if a:
    print('Reaches here.')

if not n:
    print('Reaches here.')

squared1: Optional[Int] = a.map(lambda x: x * x)                    # Optional(4)
squared2: Optional[Int] = n.map(lambda x: x * x)                    # Nil
sum1: Optional[Int] = a.flat_map(lambda x: b.map(lambda y: x + y))  # Optional(5)
sum2: Optional[Int] = a.flat_map(lambda x: n.map(lambda y: x + y))  # Nil

unwrapped: Int = a.x  # `!` in Swift: `x` of e*x*clamation marks
_ = n.x  # NilError

# `??` in Swift: `q` of *q*uestion marks
coalesced1: Int = a.qq(0)  ## 2
coalesced2: Int = n.qq(0)  ## 0

License

The MIT License

Metadata

Release files for swifpy 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for swifpy 0.0.1
File Size Uploaded
swifpy-0.0.1.tar.gz 5.2 kB Details

Release files / swifpy-0.0.1.tar.gz

Download URL swifpy-0.0.1.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
4cb60f5e081539b5b2dab2464d9117c77a2a5e0b5351cf60c48f9f2351689e31
BLAKE2b-256 checksum
How to use checksums
9b2f89b0c8a177fd9a7052c7520957cee45be03a4114b843e2494a4a67fcc5f2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.0.1 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page