Skip to content

xcodeBn/Pykot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

PyKot πŸβž‘οΈπŸ”·

Python-style extensions for Kotlin - Write Kotlin code with Python's elegant syntax!

Kotlin License: MIT

PyKot brings Python's beloved syntax and built-in functions to Kotlin, making your code more concise and expressive. No more verbose Java-style operations - embrace Python's elegance in your Kotlin projects!

✨ Features

  • String Operations: Python-style string multiplication, formatting, and methods
  • Collection Magic: List multiplication, negative indexing, and Python-like methods
  • Functional Programming: Built-in map, filter, reduce, zip functions
  • Utility Functions: len(), print(), type(), isinstance(), and more
  • Numeric Extensions: pow, abs, min, max, sum, divmod
  • Dictionary Operations: Python-style dict methods and membership testing
  • Range Functions: range() and xrange() just like Python

πŸš€ Quick Start

Installation

Add it to your build.gradle:

repositories {
    maven { url 'https://jitpack.io' }
}

dependencies {
    implementation 'com.github.xcodebn:pykot:1.0.0'
}

Usage

import com.xcodebn.pykot.*

fun main() {
    // String multiplication (Python style!)
    println("=" * 50)  // Prints 50 equals signs
    
    // List multiplication and operations
    val numbers = listOf(1, 2, 3) * 2  // [1, 2, 3, 1, 2, 3]
    println("Length: ${len(numbers)}")  // Length: 6
    
    // Negative indexing
    println("Last item: ${numbers.at(-1)}")  // Last item: 3
    
    // Python-style range
    for (i in range(5)) {  // 0, 1, 2, 3, 4
        print(i, end=" ")
    }
    
    // String formatting
    val message = "Hello {0}, you have {1} messages!".format("Alice", 5)
    println(message)  // Hello Alice, you have 5 messages!
    
    // Dictionary operations
    val myDict = dict("name" to "PyKot", "version" to "1.0.0")
    println("name" `in` myDict)  // true
    
    // Functional programming
    val doubled = map(listOf(1, 2, 3)) { it * 2 }  // [2, 4, 6]
    val evens = filter(listOf(1, 2, 3, 4)) { it % 2 == 0 }  // [2, 4]
}

πŸ“š Complete Feature Guide

String Extensions

// String multiplication
"Hi! " * 3  // "Hi! Hi! Hi! "

// String formatting
"User: {0}, Age: {1}".format("John", 25)  // "User: John, Age: 25"

// Membership testing
"world" `in` "hello world"  // true

// Python methods
"  hello  ".strip()        // "hello"
"hello".upper()            // "HELLO"
"hello".startswith("he")   // true
"hi".center(6, "*")        // "**hi**"

// Smart splitting
"a   b  c".split()         // ["a", "b", "c"]

// Joining
listOf("a", "b", "c").join("-")  // "a-b-c"

Collection Magic

// List multiplication
listOf(1, 2) * 3           // [1, 2, 1, 2, 1, 2]

// Negative indexing (Python style!)
val list = listOf("a", "b", "c", "d")
list.at(-1)                // "d" (last element)
list.at(-2)                // "c" (second to last)

// List slicing
list[1..2]                 // ["b", "c"]

// Python-style methods
val mutableList = list("a", "b", "c")
mutableList.append("d")
mutableList.extend(listOf("e", "f"))
val lastItem = mutableList.pop()  // removes and returns last

// Enumerate like Python
listOf("apple", "banana").enumerate()  // [(0, "apple"), (1, "banana")]

// List comprehensions (sort of)
listOf(1, 2, 3, 4).listcomp { it * 2 }           // [2, 4, 6, 8]
listOf(1, 2, 3, 4).listcomp({ it % 2 == 0 }) { it * 2 }  // [4, 8]

Dictionary Operations

// Create dictionaries
val myDict = dict("name" to "PyKot", "type" to "Library")

// Python-style methods
myDict.get("name", "Unknown")  // Returns value or default
myDict.setdefault("version", "1.0")  // Sets if not exists
myDict.update(dict("author" to "xcodebn"))

// Membership testing
"name" `in` myDict         // true

// Dictionary properties (Python style)
myDict.keys                // Set of keys
myDict.values              // Collection of values  
myDict.items               // Set of entries

Ranges (Just Like Python!)

// Basic ranges
range(5)           // 0, 1, 2, 3, 4
range(2, 8)        // 2, 3, 4, 5, 6, 7
range(1, 10, 2)    // [1, 3, 5, 7, 9]

// Lazy ranges (memory efficient)
xrange(1000000)    // Creates lazy sequence, not full list

// Convert to list when needed
range(5).toList()  // [0, 1, 2, 3, 4]

Utility Functions

// Length function for everything
len("hello")              // 5
len(listOf(1, 2, 3))     // 3
len(mapOf("a" to 1))     // 1

// Type checking
type("hello")             // "String"
isinstance<String>("hi") // true

// Conversions
str(123)                 // "123"
int("456")               // 456
float("3.14")            // 3.14
bool("")                 // false
bool("hello")            // true

// Object inspection
id(myObject)             // Hash code
dir(myObject)            // List of methods

// Enhanced print
print("Hello", "World", sep="-", end="!\n")  // "Hello-World!"

Numeric Operations

// Power operator
2 pow 3                  // 8
2.5 pow 2.0             // 6.25

// Math functions
abs(-42)                 // 42
min(5, 2, 8, 1)         // 1
max(5, 2, 8, 1)         // 8
round(3.14159, 2)       // 3.14

// Aggregation
sum(listOf(1, 2, 3, 4)) // 10

// Division with remainder
divmod(7, 3)            // Pair(2, 1)  β†’ 7 = 2*3 + 1

// Constants
PI                      // 3.141592653589793
E                       // 2.718281828459045

Functional Programming

// Top-level functions (Python style)
map(listOf(1, 2, 3)) { it * 2 }           // [2, 4, 6]
filter(listOf(1, 2, 3, 4)) { it % 2 == 0 } // [2, 4]
reduce(listOf(1, 2, 3, 4)) { a, b -> a + b } // 10

// Zip operations
zip(listOf(1, 2), listOf("a", "b"))       // [(1, "a"), (2, "b")]
zip(listOf(1, 2), listOf("a", "b"), listOf(true, false))  // Triple

// Boolean aggregation
any(listOf(true, false, false))           // true
all(listOf(true, true, false))            // false

// Iterator operations
val iter = iter(listOf(1, 2, 3))
next(iter)                                // 1
next(iter, -1)                           // 2 or -1 if no more

🎯 Why PyKot?

no reason i was just bored :')

Before PyKot:

val line = "=".repeat(50)
val doubled = mutableListOf<Int>()
repeat(3) { doubled.addAll(listOf(1, 2, 3)) }
val lastItem = myList[myList.size - 1]
val message = "Hello $name, you have $count messages!"

With PyKot:

val line = "=" * 50
val doubled = listOf(1, 2, 3) * 3
val lastItem = myList.at(-1)
val message = "Hello {0}, you have {1} messages!".format(name, count)

🀝 Contributing

Contributions are welcome! Here's how you can help:

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Commit your changes: git commit -m 'Add amazing feature'
  4. Push to the branch: git push origin feature/amazing-feature
  5. Open a Pull Request

Ideas for contributions:

  • More Python built-in functions
  • Additional string methods
  • Set operations
  • File I/O utilities
  • More comprehensive documentation
  • Performance optimizations

πŸ“ License

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

🌟 Show Your Support

If PyKot makes your Kotlin code more Pythonic and enjoyable, please give it a ⭐️!

πŸ“¬ Contact

xcodebn - @xcodebn

Project Link: https://github.com/xcodebn/pykot


About

No description or website provided.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages