├── .gitignore ├── LICENSE ├── README.md ├── build.gradle ├── gradle └── wrapper │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src ├── main └── java │ └── com │ └── github │ └── jabbalaci │ └── pyjava │ ├── Common.java │ ├── Py.java │ ├── PyList.java │ ├── PyStr.java │ ├── TypeError.java │ ├── ValueError.java │ ├── examples │ ├── Example01.java │ ├── Example02.java │ ├── Example03.java │ ├── Example04.java │ ├── Example05.java │ ├── Example06.java │ ├── Example07.java │ ├── Example08.java │ ├── Example09.java │ └── Example10.java │ └── proba │ └── TestPy.java └── test ├── java └── com │ └── github │ └── jabbalaci │ └── pyjava │ ├── AsciiTest.java │ ├── ConversionsTest.java │ ├── FileReadTest.java │ ├── ListTest.java │ ├── RangeTest.java │ ├── SetTest.java │ ├── StdoutTest.java │ └── StringMethodsTest.java └── resources ├── symbols.txt └── text.txt /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | build/ 3 | .classpath 4 | .project 5 | .settings/ 6 | bin/ 7 | 8 | # Mobile Tools for Java (J2ME) 9 | .mtj.tmp/ 10 | 11 | # Package Files # 12 | *.jar 13 | *.war 14 | *.ear 15 | 16 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 17 | hs_err_pid* 18 | 19 | # own settings 20 | .gradle/ 21 | .idea/ 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Jabba Laci 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | **New (Oct 17, 2020):** The project was converted to a Gradle project. 2 | It is still not maintained :) 3 | 4 | **New (Apr 13, 2018):** If you want to stay on Java land, try Kotlin 5 | or Scala. They both have the features that I tried to do in this project 6 | and they offer a lot more. Another very nice language is C# and thanks 7 | to .NET Core, it works flawlessly on Linux too. In C# I also missed some 8 | features of Python, so I have a similar project to this one. 9 | You can find it here: 10 | [https://github.com/jabbalaci/JabbaCustomExtensions-for-C-Sharp](https://github.com/jabbalaci/JabbaCustomExtensions-for-C-Sharp). 11 | 12 | **New (Oct 23, 2017):** If you want to work with Java but you need something 13 | similar to Python, I suggest [Kotlin](https://en.wikipedia.org/wiki/Kotlin_(programming_language)). 14 | Kotlin knows all these things that I implemented here. For instance, 15 | take the first example (see below): reverse the digits of an integer, 16 | and the result must also be an integer: `val res = number.toString().reversed().toInt()`. 17 | Kotlin is quite similar to Python, it runs on the JVM, much simpler than 18 | Java, and fun to use. Try Kotlin! 19 | 20 | **New (Jan 9, 2017):** This project is not maintained anymore. It was fun 21 | for 2 weeks, but then I realized I won't use it. If you want to add something 22 | to it, make a fork. Thanks. 23 | 24 | PyJava 25 | ====== 26 | 27 | PyJava is a small Java library that aims to bring some Python to Java land. 28 | If you are familiar with Python, then with PyJava you can code 29 | in Java similar to Python. PyJava tries to bring some fun to Java 30 | users. 31 | 32 | Let's start with some examples to see what it looks like. First: 33 | 34 | // Write a function that receives a whole number (integer) and 35 | // returns its reverse as a whole number. 36 | 37 | int reverse(int n) { 38 | return Py.to_int(Py.str.reverse(Py.to_str(n))); 39 | } 40 | 41 | Py.print(2016); // 2016 42 | Py.print(reverse(2016)); // 6102 43 | 44 | Second: 45 | 46 | // Using a for loop, print the lower case letters of the English alphabet: 47 | // 48 | // a b c d e f g h i j k l m n o p q r s t u v w x y z 49 | 50 | int start = Py.ord('a'); 51 | int end = Py.ord('z'); 52 | 53 | for (int i = start; i <= end; ++i) { 54 | Py.print(Py.chr(i), " "); // " " means: end=" " 55 | } 56 | Py.print(); 57 | 58 | Third: 59 | 60 | // removing duplicates 61 | // 62 | // Consider the following list: [5, 2, 3, 5, 1, 4, -200, 5, 1, 3, 2, 2, 5] . 63 | // Remove the duplicates, i.e. an element should be present in the list maximum once. 64 | // Let the result be a sorted list. 65 | 66 | List li = Py.as_list(5, 2, 3, 5, 1, 4, -200, 5, 1, 3, 2, 2, 5); 67 | Py.print(Py.sorted(Py.to_list(Py.to_set(li)))); 68 | 69 | See the `.../pyjava/examples` package for more examples. 70 | 71 | But why? 72 | -------- 73 | I know about Jython and Groovy, but I wanted to use plain Java. 74 | I also wanted something lighweight, nothing complicated, that is 75 | familiar to Python programmers. This project was made for fun, 76 | so I don't think anyone should program in Java like this :) 77 | But sometimes I need to use Java, and I always miss the 78 | Python goodies. That's why this library was born. 79 | 80 | How to use it? 81 | -------------- 82 | Simply import the class `Py`: 83 | 84 | import com.github.jabbalaci.pyjava.Py; 85 | 86 | All its functions are static, so there is no need to instantiate it. 87 | `Py` contains the built-in functions of Python (some of them). 88 | String-related functions (that are in Python's `str` class) are 89 | in `Py.str`. As an alternative, you can also use the class `PyStr`. 90 | List-related functions (that are in Python's `list` class) are 91 | in `Py.list`. As an alternative, you can also use the class `PyList`. 92 | 93 | Python to PyJava 94 | ================ 95 | 96 | Built-in functions 97 | ------------------ 98 | 99 | Python PyJava remark 100 | ====== ====== ====== 101 | 102 | print(...) Py.print(...) newline added 103 | print(..., end=" ") Py.print(..., " ") specify the end character 104 | print() Py.print() print just a newline 105 | print("...".format(...)) Py.printf(...) shortcut for System.out.printf(...) 106 | input() Py.input() read from stdin 107 | input(prompt) Py.input(prompt) add a prompt 108 | str(obj) Py.to_str(obj) try to convert anything to String 109 | int("5") Py.to_int("5") convert to int 110 | float("3.14") Py.to_float("3.14") returns a float 111 | Py.to_double("3.14") returns a double (use this) 112 | range(end) Py.range2(end) Python 2's range, i.e. returns a list 113 | range(start, end) Py.range2(start, end) 114 | range(start, end, step) Py.range2(start, end, step) 115 | li = [] List li = Py.new_list(0) 0 is the integer type 116 | List li = Py.new_list("") "" is the String type 117 | min(my_list) Py.min(my_list) 118 | max(my_list) Py.max(my_list) 119 | sum(my_list) Py.sum(my_list) works with int and double lists 120 | -- Py.prod(my_list) product of the elements 121 | works with int and double lists 122 | chr(65) Py.chr(65) ASCII value to char 123 | ord('a') Py.ord('a') char to ASCII value 124 | Py.ord("a") 125 | bin(n) Py.bin(n) 0b prefix is present 126 | -- Py.bin(n, false) 0b prefix is removed 127 | sorted(my_list) Py.sorted(my_list) returns a sorted shallow copy 128 | sorted(my_list, reverse=True) Py.sorted(my_list, true) descending order 129 | reversed(my_list) Py.reversed(my_list) returns a reversed shallow copy 130 | li = [6, 4, 3] List li = Py.as_list(6, 4, 3) 131 | li = ["aa", "bb", "cc"] List li = Py.as_list("aa", "bb", "cc") 132 | list("pyjava") Py.to_list("pyjava") explode to characters (1-long strings, actually) 133 | list(set(1, 1, 2)) Py.to_list(Set set) 134 | set([1, 3, 6]) Set bag = Py.to_set(Py.as_list(1, 3, 6)) 135 | print("-" * 20) Py.sep("-", 20) print a separator line 136 | 137 | 138 | PyStr class (available as Py.str too) 139 | ------------------------------------------ 140 | 141 | Python PyJava remark 142 | ====== ====== ====== 143 | 144 | str.capitalize("john") Py.str.capitalize("john") 145 | "pyjava"[::-1] Py.str.reverse("pyjava") 146 | "-" * 20 Py.str.repeat("-", 20) 147 | str.center("py", 10) Py.str.center("py", 10) 148 | str.split("aa:bb", ":") Py.str.split("aa:bb", ":") returns a List 149 | str.split(" a b ") Py.str.split(" a b ") split by whitespaces 150 | str.join(":", ["a", "b"]) Py.str.join(":", List li) 151 | 152 | 153 | PyList class (available as Py.list too) 154 | --------------------------------------- 155 | 156 | Python PyJava remark 157 | ====== ====== ====== 158 | 159 | my_list.sort() Py.list.sort(my_list) sort in place 160 | my_list.sort(reverse=True) Py.list.sort(my_list, true) descending order 161 | my_list.reverse() Py.list.reverse(my_list) reverse in place 162 | 163 | 164 | TODO 165 | ==== 166 | 167 | Unit tests are added but the documentation is not yet perfect. Generating the javadoc 168 | (with `maven javadoc:javadoc` for instance) fails. Contributions are welcome here :) 169 | 170 | Contributors 171 | ------------ 172 | 173 | [Peter Jeszenszky](https://github.com/jeszy75) 174 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | } 4 | 5 | group 'org.example' 6 | version '1.0-SNAPSHOT' 7 | 8 | repositories { 9 | mavenCentral() 10 | } 11 | 12 | dependencies { 13 | testCompile group: 'junit', name: 'junit', version: '4.12' 14 | } 15 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | 86 | # Determine the Java command to use to start the JVM. 87 | if [ -n "$JAVA_HOME" ] ; then 88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 89 | # IBM's JDK on AIX uses strange locations for the executables 90 | JAVACMD="$JAVA_HOME/jre/sh/java" 91 | else 92 | JAVACMD="$JAVA_HOME/bin/java" 93 | fi 94 | if [ ! -x "$JAVACMD" ] ; then 95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 96 | 97 | Please set the JAVA_HOME variable in your environment to match the 98 | location of your Java installation." 99 | fi 100 | else 101 | JAVACMD="java" 102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 103 | 104 | Please set the JAVA_HOME variable in your environment to match the 105 | location of your Java installation." 106 | fi 107 | 108 | # Increase the maximum file descriptors if we can. 109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 110 | MAX_FD_LIMIT=`ulimit -H -n` 111 | if [ $? -eq 0 ] ; then 112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 113 | MAX_FD="$MAX_FD_LIMIT" 114 | fi 115 | ulimit -n $MAX_FD 116 | if [ $? -ne 0 ] ; then 117 | warn "Could not set maximum file descriptor limit: $MAX_FD" 118 | fi 119 | else 120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 121 | fi 122 | fi 123 | 124 | # For Darwin, add options to specify how the application appears in the dock 125 | if $darwin; then 126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 127 | fi 128 | 129 | # For Cygwin or MSYS, switch paths to Windows format before running java 130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then 131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 133 | 134 | JAVACMD=`cygpath --unix "$JAVACMD"` 135 | 136 | # We build the pattern for arguments to be converted via cygpath 137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 138 | SEP="" 139 | for dir in $ROOTDIRSRAW ; do 140 | ROOTDIRS="$ROOTDIRS$SEP$dir" 141 | SEP="|" 142 | done 143 | OURCYGPATTERN="(^($ROOTDIRS))" 144 | # Add a user-defined pattern to the cygpath arguments 145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 147 | fi 148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 149 | i=0 150 | for arg in "$@" ; do 151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 153 | 154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 156 | else 157 | eval `echo args$i`="\"$arg\"" 158 | fi 159 | i=`expr $i + 1` 160 | done 161 | case $i in 162 | 0) set -- ;; 163 | 1) set -- "$args0" ;; 164 | 2) set -- "$args0" "$args1" ;; 165 | 3) set -- "$args0" "$args1" "$args2" ;; 166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;; 167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 172 | esac 173 | fi 174 | 175 | # Escape application args 176 | save () { 177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 178 | echo " " 179 | } 180 | APP_ARGS=`save "$@"` 181 | 182 | # Collect all arguments for the java command, following the shell quoting and substitution rules 183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 184 | 185 | exec "$JAVACMD" "$@" 186 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto init 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto init 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :init 68 | @rem Get command-line arguments, handling Windows variants 69 | 70 | if not "%OS%" == "Windows_NT" goto win9xME_args 71 | 72 | :win9xME_args 73 | @rem Slurp the command line arguments. 74 | set CMD_LINE_ARGS= 75 | set _SKIP=2 76 | 77 | :win9xME_args_slurp 78 | if "x%~1" == "x" goto execute 79 | 80 | set CMD_LINE_ARGS=%* 81 | 82 | :execute 83 | @rem Setup the command line 84 | 85 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 86 | 87 | 88 | @rem Execute Gradle 89 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 90 | 91 | :end 92 | @rem End local scope for the variables with windows NT shell 93 | if "%ERRORLEVEL%"=="0" goto mainEnd 94 | 95 | :fail 96 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 97 | rem the _cmd.exe /c_ return code! 98 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 99 | exit /b 1 100 | 101 | :mainEnd 102 | if "%OS%"=="Windows_NT" endlocal 103 | 104 | :omega 105 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'PyJava' 2 | 3 | -------------------------------------------------------------------------------- /src/main/java/com/github/jabbalaci/pyjava/Common.java: -------------------------------------------------------------------------------- 1 | package com.github.jabbalaci.pyjava; 2 | 3 | public class Common { 4 | 5 | /** 6 | * Used to compare two doubles in the tests. 7 | */ 8 | public static double TOLERANCE = 0.0001; 9 | 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/github/jabbalaci/pyjava/Py.java: -------------------------------------------------------------------------------- 1 | package com.github.jabbalaci.pyjava; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.nio.charset.Charset; 6 | import java.nio.file.Files; 7 | import java.nio.file.Path; 8 | import java.nio.file.Paths; 9 | 10 | /** 11 | PyJava 12 | ====== 13 | 14 | Pythonize Java to make it more fun to use. 15 | 16 | Here I implement(ed) some features of Python that I miss a lot when using Java. 17 | 18 | Author: Laszlo Szathmary, alias Jabba Laci, 2016 19 | E-mail: jabba.laci@gmail.com 20 | GitHub: https://github.com/jabbalaci 21 | */ 22 | 23 | import java.util.ArrayList; 24 | import java.util.Collection; 25 | import java.util.Collections; 26 | import java.util.HashSet; 27 | import java.util.Iterator; 28 | import java.util.List; 29 | import java.util.ListIterator; 30 | import java.util.Scanner; 31 | import java.util.Set; 32 | 33 | 34 | public class Py { 35 | 36 | /** 37 | * Access things in PyStr as (examples): 38 | * 1) PyStr.reverse(...) , or 39 | * 2) Py.str.reverse(...) 40 | */ 41 | public static PyStr str; 42 | 43 | /** 44 | * Access things in PyList as (examples): 45 | * 1) PyList.sort(...) , or 46 | * 2) Py.list.sort(...) 47 | */ 48 | public static PyList list; 49 | 50 | /** 51 | * For reading from the stdin. 52 | */ 53 | private static Scanner scanner = new Scanner(System.in); 54 | 55 | // ---------------------------------------------------------------------- 56 | // System.out.print* 57 | // ---------------------------------------------------------------------- 58 | 59 | /** 60 | * shortcut for System.out.println 61 | * 62 | * @param o An object you want to print. 63 | */ 64 | public static void print(Object o) { 65 | System.out.println(o); 66 | } 67 | 68 | /** 69 | * In Python, print's help looks like this: 70 | * print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False) 71 | * sep could work if we used variable number or arguments and print them 72 | * one by one, but then how to recognize the optional parameters at the end? 73 | * So I skipped sep and went with end. If end is given, then it's printed at 74 | * the end instead of a newline. 75 | * 76 | * @param o An object to print. 77 | * @param end Print this after the object. 78 | */ 79 | public static void print(Object o, String end) { 80 | System.out.print(o); 81 | System.out.print(end); 82 | } 83 | 84 | /** 85 | * Print a newline. 86 | */ 87 | public static void print() { 88 | System.out.println(); 89 | } 90 | 91 | /** 92 | * shortcut for System.out.printf(...) 93 | * 94 | * @param format 95 | * @param args 96 | */ 97 | public static void printf(String format, Object ... args) { 98 | System.out.printf(format, args); 99 | } 100 | 101 | // ---------------------------------------------------------------------- 102 | // type conversions 103 | // ---------------------------------------------------------------------- 104 | 105 | /** 106 | * @param o An object. 107 | * @return Return a string containing a nicely printable representation of an object. 108 | */ 109 | public static String to_str(T o) { 110 | return "" + o; 111 | } 112 | 113 | /** 114 | * @param s A string. 115 | * @return Integer version of the string. 116 | */ 117 | public static int to_int(String s) { 118 | return Integer.parseInt(s); 119 | } 120 | 121 | /** 122 | * This function cannot be called "double", that's why it is named as "to_double". 123 | * I suggest using this instead of "to_float". 124 | * 125 | * @param s A string. 126 | * @return Double version of the string. 127 | */ 128 | public static double to_double(String s) { 129 | return Double.parseDouble(s); 130 | } 131 | 132 | /** 133 | * This function cannot be called "float", that's why it is named as "to_float". 134 | * I suggest using "to_double" instead. 135 | * 136 | * @param s A string. 137 | * @return Float version of the string. 138 | */ 139 | public static float to_float(String s) { 140 | return Float.parseFloat(s); 141 | } 142 | 143 | // ---------------------------------------------------------------------- 144 | // reading from stdin 145 | // ---------------------------------------------------------------------- 146 | 147 | /** 148 | * @return Read a line from stdin and return the line as a string. 149 | */ 150 | public static String input() { 151 | return Py.scanner.nextLine(); 152 | } 153 | 154 | /** 155 | * @param prompt A prompt. 156 | * @return Print the prompt, read a line from stdin and return the line as a string. 157 | */ 158 | public static String input(String prompt) { 159 | System.out.print(prompt); 160 | return Py.scanner.nextLine(); 161 | } 162 | 163 | // ---------------------------------------------------------------------- 164 | // Python 2's range 165 | // ---------------------------------------------------------------------- 166 | 167 | /** 168 | * In the name "range2" the "2" shows that it's an implementation 169 | * of Python 2's range, i.e. it creates a new list. 170 | * Don't use it for big ranges! 171 | * 172 | * @param end End position. 173 | * @return A ranged list from 0 (included) to end (excluded). 174 | */ 175 | public static List range2(int end) { 176 | List li = new ArrayList<>(); 177 | // 178 | for (int i = 0; i < end; ++i) { 179 | li.add(i); 180 | } 181 | // 182 | return li; 183 | } 184 | 185 | /** 186 | * In the name "range2" the "2" shows that it's an implementation 187 | * of Python 2's range, i.e. it creates a new list. 188 | * Don't use it for big ranges! 189 | * 190 | * @param start Start position. 191 | * @param end End position. 192 | * @return A ranged list from start (included) to end (excluded). 193 | */ 194 | public static List range2(int start, int end) { 195 | List li = new ArrayList<>(); 196 | // 197 | for (int i = start; i < end; ++i) { 198 | li.add(i); 199 | } 200 | // 201 | return li; 202 | } 203 | 204 | /** 205 | * In the name "range2" the "2" shows that it's an implementation 206 | * of Python 2's range, i.e. it creates a new list. 207 | * Don't use it for big ranges! 208 | * 209 | * @param start Start position. 210 | * @param end End position. 211 | * @param step Step value. 212 | * @return A ranged list from start (included) to end (excluded) that contains every "step"th value. 213 | */ 214 | public static List range2(int start, int end, int step) { 215 | List li = new ArrayList<>(); 216 | // 217 | if (step > 0) { 218 | for (int i = start; i < end; i += step) { 219 | li.add(i); 220 | } 221 | } 222 | else if (step < 0) { 223 | for (int i = start; i > end; i += step) { 224 | li.add(i); 225 | } 226 | } 227 | else { 228 | throw new ValueError("range() step argument must not be zero"); 229 | } 230 | // 231 | return li; 232 | } 233 | 234 | // ---------------------------------------------------------------------- 235 | // create a new empty list 236 | // ---------------------------------------------------------------------- 237 | 238 | /** 239 | * Provide a type and a new list of that type is returned. Examples: 240 | * 241 | * List = Py.new_list(0); // where 0 is an integer => create a list of integers 242 | * List = Py.new_list(""); // where "" is a string => create a list of strings 243 | * 244 | * @param type A type. 245 | * @return A new ArrayList of the given type. 246 | */ 247 | public static List new_list(T type) { 248 | return new ArrayList(); 249 | } 250 | 251 | // ---------------------------------------------------------------------- 252 | // max, min, sum, prod 253 | // ---------------------------------------------------------------------- 254 | 255 | /** 256 | * @param li A list. 257 | * @return Maximal element of the list. 258 | */ 259 | public static > T max(Collection li) { 260 | if (li.isEmpty()) { 261 | throw new ValueError("max() arg is an empty sequence"); 262 | } 263 | return Collections.max(li); 264 | } 265 | 266 | /** 267 | * @param li A list. 268 | * @return Minimal element of the list. 269 | */ 270 | public static > T min(Collection li) { 271 | if (li.isEmpty()) { 272 | throw new ValueError("min() arg is an empty sequence"); 273 | } 274 | return Collections.min(li); 275 | } 276 | 277 | /** 278 | * @param li A list. 279 | * @param type A type. It indicates the type of the elements in the list. 280 | * @return Sum of the elements in the list. 281 | */ 282 | public static int sum(List li, int type) { 283 | // requires Java 8 284 | return li.stream().mapToInt(Integer::intValue).sum(); 285 | } 286 | 287 | /** 288 | * @param li A list. 289 | * @param type A type. It indicates the type of the elements in the list. 290 | * @return Sum of the elements in the list. 291 | */ 292 | public static double sum(List li, double type) { 293 | // requires Java 8 294 | return li.stream().mapToDouble(Double::doubleValue).sum(); 295 | } 296 | 297 | /** 298 | * There's no such built-in function in Python but I thought 299 | * it would be useful. If there is "sum", there should be a "prod" too 300 | * IMO. 301 | * 302 | * @param li A list. 303 | * @param type A type. It indicates the type of the elements in the list. 304 | * @return Product of the elements in the list. 305 | */ 306 | public static int prod(List li, int type) { 307 | int total = 1; 308 | for (int e : li) { 309 | total *= e; 310 | } 311 | return total; 312 | } 313 | 314 | /** 315 | * @param li A list. 316 | * @param type A type. It indicates the type of the elements in the list. 317 | * @return Product of the elements in the list. 318 | */ 319 | public static double prod(List li, double type) { 320 | double total = 1.0; 321 | for (double e : li) { 322 | total *= e; 323 | } 324 | return total; 325 | } 326 | 327 | // ---------------------------------------------------------------------- 328 | // ASCII stuff: chr, ord 329 | // ---------------------------------------------------------------------- 330 | 331 | /** 332 | * @param i An ASCII-value. 333 | * @return The string version of a character whose ASCII code is the integer i. 334 | */ 335 | public static String chr(int i) { 336 | // Return the string representing a character 337 | // whose Unicode code point is the integer i. 338 | if ((i < 0) || (i > 255)) { 339 | throw new ValueError("chr() arg not in range(256)"); 340 | } 341 | return Character.toString((char)i); 342 | } 343 | 344 | /** 345 | * @param c A character. 346 | * @return An integer representing the Unicode code point of the given character. 347 | */ 348 | public static int ord(char c) { 349 | return (int)c; 350 | } 351 | 352 | /** 353 | * @param s A 1-long string. 354 | * @return An integer representing the Unicode code point of the given 1-long string. 355 | */ 356 | public static int ord(String s) { 357 | if (s.length() != 1) { 358 | String msg = String.format("TypeError: ord() expected a character, but string of length %d found", s.length()); 359 | throw new TypeError(msg); 360 | } 361 | return Py.ord(s.charAt(0)); 362 | } 363 | 364 | // ---------------------------------------------------------------------- 365 | // bin, hex 366 | // ---------------------------------------------------------------------- 367 | 368 | /** 369 | * Convert decimal to binary. Python adds the "0b" prefix, so 370 | * it was kept. 371 | * 372 | * @param n 373 | * @return 374 | */ 375 | public static String bin(int n) { 376 | String pre = ""; 377 | if (n < 0) { 378 | pre = "-"; 379 | n *= -1; 380 | } 381 | return pre + "0b" + Integer.toString(n, 2); 382 | } 383 | 384 | /** 385 | * Python doesn't have it. If prefix is false, then 386 | * the prefix is not added. 387 | * Rationale: in Python I always cut off the prefix... 388 | * 389 | * @param n A decimal number. 390 | * @param prefix Add the prefix or not. 391 | * @return Binary version of the decimal number. 392 | */ 393 | public static String bin(int n, boolean prefix) { 394 | if (prefix == false) { 395 | return Integer.toString(n, 2); 396 | } 397 | // else 398 | return Py.bin(n); 399 | } 400 | 401 | // ---------------------------------------------------------------------- 402 | // sorted, reversed 403 | // ---------------------------------------------------------------------- 404 | 405 | /** 406 | * It makes a shallow (!) copy of the list, sorts the copy, and returns 407 | * the sorted copy. 408 | * 409 | * @param li A list. 410 | * @return A sorted (shallow) copy of the list. 411 | */ 412 | public static > List sorted(List li) { 413 | // it makes a shallow copy 414 | List copy = new ArrayList<>(li); 415 | Collections.sort(copy); 416 | return copy; 417 | } 418 | 419 | /** 420 | * @param li 421 | * @param reverse If true, then reverse order is applied. 422 | * @return 423 | */ 424 | public static > List sorted(List li, boolean reverse) { 425 | if (reverse == false) { 426 | return Py.sorted(li); 427 | } 428 | // else, if reverse == true 429 | // it makes a shallow copy 430 | List copy = new ArrayList<>(li); 431 | Collections.sort(copy, Collections.reverseOrder()); 432 | return copy; 433 | } 434 | 435 | /** 436 | * It makes a shallow (!) copy of the list, reverses the copy, and returns 437 | * the reversed copy. 438 | * 439 | * @param li A list. 440 | * @return A reversed (shallow) copy of the list. 441 | */ 442 | public static > List reversed(List li) { 443 | // it makes a shallow copy 444 | List copy = new ArrayList<>(li); 445 | Collections.reverse(copy); 446 | return copy; 447 | } 448 | 449 | // ---------------------------------------------------------------------- 450 | // list(...) 451 | // ---------------------------------------------------------------------- 452 | 453 | /** 454 | * Take the elements and put them in a list. 455 | * 456 | * @param params Variable number of parameters. 457 | * @return The given parameters put in a List. 458 | */ 459 | @SafeVarargs 460 | public static List as_list(T... params) { 461 | List li = new ArrayList<>(); 462 | for (int i = 0; i < params.length; ++i) { 463 | li.add(params[i]); 464 | } 465 | return li; 466 | } 467 | 468 | /** 469 | * Explode a string to a list of characters. 470 | * The characters in the list are stored as 471 | * 1-long Strings. 472 | * 473 | * @param s The string to be exploded. 474 | * @return A list of 1-long Strings that are the 475 | * characters of the input string. 476 | */ 477 | public static List to_list(String s) { 478 | List li = new ArrayList<>(); 479 | for (char c : s.toCharArray()) { 480 | li.add("" + c); 481 | } 482 | return li; 483 | } 484 | 485 | /** 486 | * set -> list 487 | * 488 | * @param set An arbitrary set. 489 | * @return A new list that contains the elements of the set. 490 | */ 491 | public static List to_list(Set set) { 492 | List li = new ArrayList<>(); 493 | for (T e : set) { 494 | li.add(e); 495 | } 496 | return li; 497 | } 498 | 499 | // ---------------------------------------------------------------------- 500 | // set(...) 501 | // ---------------------------------------------------------------------- 502 | 503 | /** 504 | * list -> set 505 | * 506 | * @param li An arbitrary list. 507 | * @return A new set that contains the elements of the list. 508 | */ 509 | public static Set to_set(List li) { 510 | Set set = new HashSet<>(); 511 | for (T e : li) { 512 | set.add(e); 513 | } 514 | return set; 515 | } 516 | 517 | // ---------------------------------------------------------------------- 518 | // print a separator line 519 | // ---------------------------------------------------------------------- 520 | 521 | /** 522 | * Take the given string "rep" times and concatenate the pieces into one string. 523 | * Then print this string to the stdout. Goal: draw a separator line. 524 | * 525 | * @param s A string. 526 | * @param rep Repetition number. 527 | */ 528 | public static void sep(String s, int rep) { 529 | System.out.println(PyStr.repeat(s, rep)); 530 | } 531 | 532 | // ---------------------------------------------------------------------- 533 | // file reading 534 | // ---------------------------------------------------------------------- 535 | 536 | /** 537 | * Read a text file and return its lines in a list. 538 | * Don't use it on a big file! 539 | * 540 | * UTF-8 character encoding will be used by default. 541 | * Newline is not included at the end of the lines. 542 | * 543 | * @param fpath Path of the input file. 544 | * @return A list of lines. 545 | */ 546 | public static List readlines(String fpath) { 547 | return Py.readlines(fpath, "UTF-8"); 548 | } 549 | 550 | /** 551 | * Read a text file and return its lines in a list. 552 | * Don't use it on a big file! 553 | * 554 | * Newline is not included at the end of the lines. 555 | * 556 | * @param fpath Path of the input file. 557 | * @param encoding Character encoding of the file. 558 | * @return A list of lines. 559 | */ 560 | public static List readlines(String fpath, String encoding) { 561 | Charset charset = Charset.forName(encoding); 562 | Path p = Paths.get(fpath); 563 | String line; 564 | List res = new ArrayList(); 565 | 566 | try (BufferedReader reader = Files.newBufferedReader(p, charset)) { 567 | while ((line = reader.readLine()) != null ) { 568 | res.add(line); 569 | } 570 | } catch (IOException e) { 571 | System.err.println(e); 572 | return null; 573 | } 574 | 575 | return res; 576 | } 577 | 578 | /** 579 | * Read the file and return its content in a string. 580 | * Don't use it on a big file! 581 | * 582 | * @param fpath Path of the input file. 583 | * @return Content of the file in a string. 584 | */ 585 | public static String read(String fpath) { 586 | return Py.read(fpath, "UTF-8"); 587 | } 588 | 589 | /** 590 | * Read the file and return its content in a string. 591 | * Don't use it on a big file! 592 | * 593 | * @param fpath Path of the input file. 594 | * @param encoding Character encoding of the file. 595 | * @return Content of the file in a string. 596 | */ 597 | public static String read(String fpath, String encoding) { 598 | Charset charset = Charset.forName(encoding); 599 | Path p = Paths.get(fpath); 600 | String line; 601 | StringBuilder sb = new StringBuilder(); 602 | 603 | try (BufferedReader reader = Files.newBufferedReader(p, charset)) { 604 | while ((line = reader.readLine()) != null ) { 605 | sb.append(line); 606 | sb.append('\n'); 607 | } 608 | } catch (IOException e) { 609 | System.err.println(e); 610 | return null; 611 | } 612 | 613 | return sb.toString(); 614 | } 615 | 616 | } 617 | -------------------------------------------------------------------------------- /src/main/java/com/github/jabbalaci/pyjava/PyList.java: -------------------------------------------------------------------------------- 1 | package com.github.jabbalaci.pyjava; 2 | 3 | import java.util.Collections; 4 | 5 | /** 6 | * List methods. 7 | * List helpers. 8 | * 9 | * @author jabba 10 | */ 11 | 12 | import java.util.List; 13 | 14 | 15 | public class PyList { 16 | 17 | /** 18 | * Sort the list in place. It has no return value. 19 | * 20 | * @param li A list to be sorted. 21 | */ 22 | public static > void sort(List li) { 23 | Collections.sort(li); 24 | } 25 | 26 | /** 27 | * Sort the list in place. It has no return value. 28 | * 29 | * @param li The list to be sorted. 30 | * @param reverse If set to true, then the list is sorted in 31 | * descending order. 32 | */ 33 | public static > void sort(List li, boolean reverse) { 34 | if (reverse == false) { 35 | sort(li); 36 | return; 37 | } 38 | // else, if reverse == true 39 | Collections.sort(li, Collections.reverseOrder()); 40 | } 41 | 42 | /** 43 | * Reverse the list in place. It has no return value. 44 | * 45 | * @param li A list to be reversed. 46 | */ 47 | public static > void reverse(List li) { 48 | Collections.reverse(li); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/github/jabbalaci/pyjava/PyStr.java: -------------------------------------------------------------------------------- 1 | package com.github.jabbalaci.pyjava; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.List; 6 | 7 | /** 8 | * String methods. 9 | * 10 | * @author jabba 11 | */ 12 | 13 | public class PyStr { 14 | 15 | /** 16 | * @param s A string. 17 | * @return Capitalize the string. 18 | */ 19 | public static String capitalize(String s) { 20 | if (s.isEmpty()) { 21 | return ""; 22 | } 23 | String first = "" + s.charAt(0); // 1-long string 24 | String rest = s.substring(1); 25 | 26 | return first.toUpperCase() + rest.toLowerCase(); 27 | } 28 | 29 | /** 30 | * @param s A string. 31 | * @return Reverse the order of characters in the string and return the reversed string. 32 | */ 33 | public static String reverse(String s) { 34 | return new StringBuilder(s).reverse().toString(); 35 | } 36 | 37 | /** 38 | * @param s A string. 39 | * @param rep Repetition number. 40 | * @return Take the given string "rep" times and concatenate the pieces into one string. 41 | */ 42 | public static String repeat(String s, int rep) { 43 | StringBuilder sb = new StringBuilder(); 44 | for (int i = 0; i < rep; ++i) { 45 | sb.append(s); 46 | } 47 | return sb.toString(); 48 | } 49 | 50 | /** 51 | * Center a text of a given width. 52 | * 53 | * @param s A string to be centered. 54 | * @param width A width. The text must go to the middle of this width. 55 | * @return A string where the given text is centered of a given width. 56 | */ 57 | public static String center(String s, int width) { 58 | int rest = width - s.length(); 59 | int right = rest / 2; 60 | int left = rest - right; 61 | return repeat(" ", left) + s + repeat(" ", right); 62 | } 63 | 64 | /** 65 | * Split a string by a separator string. 66 | * The return value is a list. 67 | * 68 | * @param s A string. 69 | * @param sep A separator string (can be 1-long). 70 | * @return The exploded (split up) string. 71 | */ 72 | public static List split(String s, String sep) { 73 | String[] parts = s.split(sep); 74 | return Arrays.asList(parts); 75 | } 76 | 77 | /** 78 | * Split a string by whitespace characters. 79 | * 80 | * @param s A string. 81 | * @return The exploded (split up) string. 82 | */ 83 | public static List split(String s) { 84 | String sep = "\\s+"; // regexp for whitespace 85 | String[] parts = s.split(sep); 86 | List li = new ArrayList<>(); 87 | for (String p : parts) { 88 | if (p.isEmpty() == false) { 89 | li.add(p); 90 | } 91 | } 92 | return li; 93 | } 94 | 95 | public static String join(String sep, List li) { 96 | return String.join(sep, li); 97 | } 98 | 99 | } 100 | -------------------------------------------------------------------------------- /src/main/java/com/github/jabbalaci/pyjava/TypeError.java: -------------------------------------------------------------------------------- 1 | package com.github.jabbalaci.pyjava; 2 | 3 | @SuppressWarnings("serial") 4 | public class TypeError extends RuntimeException { 5 | public TypeError(String message) { 6 | super(message); 7 | } 8 | } -------------------------------------------------------------------------------- /src/main/java/com/github/jabbalaci/pyjava/ValueError.java: -------------------------------------------------------------------------------- 1 | package com.github.jabbalaci.pyjava; 2 | 3 | /* 4 | * "You can throw unchecked exceptions without having to declare them 5 | * if you really want to. Unchecked exceptions extend RuntimeException." 6 | * http://stackoverflow.com/a/4519576/232485 7 | */ 8 | @SuppressWarnings("serial") 9 | public class ValueError extends RuntimeException { 10 | public ValueError(String message) { 11 | super(message); 12 | } 13 | } -------------------------------------------------------------------------------- /src/main/java/com/github/jabbalaci/pyjava/examples/Example01.java: -------------------------------------------------------------------------------- 1 | package com.github.jabbalaci.pyjava.examples; 2 | 3 | /* 4 | Write a function that receives a whole number (integer) and returns its reverse as a whole number. 5 | 6 | Examples: 1977 → 7791; 12568 → 86521. 7 | */ 8 | 9 | import com.github.jabbalaci.pyjava.Py; 10 | 11 | public class Example01 { 12 | 13 | public static void main(String[] args) { 14 | Example01 m = new Example01(); 15 | m.start(); 16 | } 17 | 18 | private int reverse(int n) { 19 | return Py.to_int(Py.str.reverse(Py.to_str(n))); 20 | } 21 | 22 | private void start() { 23 | int n = 1977; 24 | int reversed = reverse(n); 25 | Py.print(n); 26 | Py.print(reversed); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/github/jabbalaci/pyjava/examples/Example02.java: -------------------------------------------------------------------------------- 1 | package com.github.jabbalaci.pyjava.examples; 2 | 3 | /* 4 | sum of natural numbers from 1 to 100 5 | 6 | It's not an efficient implementation since it builds 7 | a list of 100 elements. It simply demonstrates the range2 function, 8 | which is the equivalent version of Python 2's range. 9 | */ 10 | 11 | import com.github.jabbalaci.pyjava.Py; 12 | 13 | public class Example02 { 14 | 15 | public static void main(String[] args) { 16 | Example02 m = new Example02(); 17 | m.start(); 18 | } 19 | 20 | private void start() { 21 | // sum(list, type_of_elems_in_the_list) 22 | // sum(list, 0) -> the list contains integers 23 | Py.print(Py.sum(Py.range2(100+1), 0)); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/github/jabbalaci/pyjava/examples/Example03.java: -------------------------------------------------------------------------------- 1 | package com.github.jabbalaci.pyjava.examples; 2 | 3 | /* 4 | Using a for loop, print the lower case letters of the English alphabet: 5 | 6 | a b c d e f g h i j k l m n o p q r s t u v w x y z 7 | 8 | As can be seen, there is a space between the letters. 9 | */ 10 | 11 | import com.github.jabbalaci.pyjava.Py; 12 | 13 | public class Example03 { 14 | 15 | public static void main(String[] args) { 16 | Example03 m = new Example03(); 17 | m.start(); 18 | } 19 | 20 | private void start() { 21 | int start = Py.ord('a'); 22 | int end = Py.ord('z'); 23 | 24 | for (int i = start; i <= end; ++i) { 25 | Py.print(Py.chr(i), " "); // " " means: end=" " 26 | } 27 | Py.print(); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/github/jabbalaci/pyjava/examples/Example04.java: -------------------------------------------------------------------------------- 1 | package com.github.jabbalaci.pyjava.examples; 2 | 3 | import java.util.List; 4 | 5 | /* 6 | Write a method that receives the height of a diamond. 7 | The method will have to print a diamond the following way: 8 | 9 | Height: 3 10 | 11 | * 12 | *** 13 | * 14 | 15 | Height: 7 16 | 17 | * 18 | *** 19 | ***** 20 | ******* 21 | ***** 22 | *** 23 | * 24 | 25 | Accept odd numbers only for the value of the height. 26 | */ 27 | 28 | import com.github.jabbalaci.pyjava.Py; 29 | import com.github.jabbalaci.pyjava.PyStr; 30 | 31 | public class Example04 { 32 | 33 | public static void main(String[] args) { 34 | Example04 m = new Example04(); 35 | m.diamond(7); 36 | } 37 | 38 | private void diamond(int height) { 39 | List li = Py.range2(1, height+1, 2); 40 | List li2 = Py.reversed(li.subList(0, li.size()-1)); 41 | li.addAll(li2); 42 | Py.print(li); 43 | 44 | for (int n : li) { 45 | Py.print(PyStr.center(PyStr.repeat("*", n), height)); 46 | } 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/github/jabbalaci/pyjava/examples/Example05.java: -------------------------------------------------------------------------------- 1 | package com.github.jabbalaci.pyjava.examples; 2 | 3 | /* 4 | An anagram is a type of word play, the result of rearranging 5 | the letters of a word or phrase to produce a new word or phrase, 6 | using all the original letters exactly once. 7 | 8 | Examples: 9 | 10 | listen = silent 11 | A gentleman = Elegant man 12 | Clint Eastwood = Old west action 13 | 14 | Write a function that receives two strings and decides 15 | if the second string is an anagram of the first one. 16 | As you can see from the examples, whitespaces and upper and 17 | lower case letters don't matter. 18 | */ 19 | 20 | import com.github.jabbalaci.pyjava.Py; 21 | 22 | public class Example05 { 23 | 24 | public static void main(String[] args) { 25 | Example05 m = new Example05(); 26 | m.start(); 27 | } 28 | 29 | private String clean(String s) { 30 | return s.replace(" ", "").toLowerCase(); 31 | } 32 | 33 | private void start() { 34 | String s1_original = "A gentleman"; 35 | String s2_original = "Elegant man"; 36 | 37 | String s1 = clean(s1_original); 38 | String s2 = clean(s2_original); 39 | 40 | boolean res = Py.sorted(Py.to_list(s1)).equals(Py.sorted(Py.to_list(s2))); 41 | Py.printf("Anagramma(%s, %s): %b", s1_original, 42 | s2_original, 43 | res); 44 | Py.print(); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/com/github/jabbalaci/pyjava/examples/Example06.java: -------------------------------------------------------------------------------- 1 | package com.github.jabbalaci.pyjava.examples; 2 | 3 | /* 4 | Palindrome 5 | 6 | Write a function that decides about a string if it's a palindrome. 7 | A string is said to be palindrome if it reads the same backwards as 8 | forwards, e.g. madam. 9 | */ 10 | 11 | import com.github.jabbalaci.pyjava.Py; 12 | import com.github.jabbalaci.pyjava.PyStr; 13 | 14 | public class Example06 { 15 | 16 | public static void main(String[] args) { 17 | Example06 m = new Example06(); 18 | m.start(); 19 | } 20 | 21 | private boolean is_palindrome(String s) { 22 | return s.equals(PyStr.reverse(s)); 23 | } 24 | 25 | private void start() { 26 | String s = "madam"; 27 | Py.printf("%s is palindrome: %b", s, is_palindrome(s)); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/github/jabbalaci/pyjava/examples/Example07.java: -------------------------------------------------------------------------------- 1 | package com.github.jabbalaci.pyjava.examples; 2 | 3 | /* 4 | ASCII table 5 | 6 | Print the ASCII table in the following form (sample): 7 | 8 | ... 9 | 55: 7 10 | 56: 8 11 | 57: 9 12 | 58: : 13 | 59: ; 14 | 60: < 15 | 61: = 16 | 62: > 17 | 63: ? 18 | 64: @ 19 | 65: A 20 | 66: B 21 | 67: C 22 | 68: D 23 | ... 24 | 25 | It's enough to print from the 32-127 interval, since the characters 26 | from 0 to 31 are special, non-printable characters. 27 | 28 | Tip: use the chr built-in function. Example: chr(97) → 'a'. 29 | 30 | Extra Question 31 | -------------- 32 | 33 | What is the sum of the ASCII values of the upper case letters in the English alphabet? 34 | */ 35 | 36 | import com.github.jabbalaci.pyjava.Py; 37 | 38 | public class Example07 { 39 | 40 | public static void main(String[] args) { 41 | Example07 m = new Example07(); 42 | // m.start(); 43 | m.extra(); 44 | } 45 | 46 | private void extra() { 47 | int total = 0; 48 | for (int i = Py.ord("A"); i <= Py.ord("Z"); ++i) { 49 | // Py.print(Py.chr(i)); 50 | total += i; 51 | } 52 | Py.print(total); 53 | } 54 | 55 | @SuppressWarnings("unused") 56 | private void start() { 57 | for (int i = 32; i < 128; ++i) { 58 | Py.printf("%d: %s\n", i, Py.chr(i)); 59 | } 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/com/github/jabbalaci/pyjava/examples/Example08.java: -------------------------------------------------------------------------------- 1 | package com.github.jabbalaci.pyjava.examples; 2 | 3 | /* 4 | Sentence without extra spaces. 5 | 6 | test('I love Python') == "I love Python" 7 | 8 | */ 9 | 10 | import com.github.jabbalaci.pyjava.Py; 11 | import com.github.jabbalaci.pyjava.PyStr; 12 | 13 | public class Example08 { 14 | 15 | public static void main(String[] args) { 16 | Example08 m = new Example08(); 17 | m.start(); 18 | } 19 | 20 | private void start() { 21 | String s = "I love Python"; 22 | Py.print("'" + s + "'"); 23 | String t = PyStr.join(" ", PyStr.split(s)); 24 | Py.print("'" + t + "'"); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/github/jabbalaci/pyjava/examples/Example09.java: -------------------------------------------------------------------------------- 1 | package com.github.jabbalaci.pyjava.examples; 2 | 3 | /* 4 | Double-base palindromes 5 | 6 | The decimal number, 585 = 10010010012 (binary), is palindromic in both bases. 7 | 8 | Find the sum of all numbers, less than one million, which are palindromic 9 | in base 10 and base 2. 10 | 11 | (Please note that the palindromic number, in either base, may not include leading zeros.) 12 | 13 | */ 14 | 15 | import com.github.jabbalaci.pyjava.Py; 16 | import com.github.jabbalaci.pyjava.PyStr; 17 | 18 | public class Example09 { 19 | 20 | private static final int UPTO = 1000000; 21 | 22 | public static void main(String[] args) { 23 | Example09 m = new Example09(); 24 | m.start(); 25 | } 26 | 27 | private boolean is_palindrome(String s) { 28 | return s.equals(PyStr.reverse(s)); 29 | } 30 | 31 | private boolean palindrome_base_2(int n) { 32 | return is_palindrome(Py.bin(n, false)); 33 | } 34 | 35 | private boolean palindrome_base_10(int n) { 36 | return is_palindrome(Py.to_str(n)); 37 | } 38 | 39 | private void start() { 40 | int total = 0; 41 | 42 | for (int i = 0; i < UPTO; ++i) { 43 | if (palindrome_base_10(i) && palindrome_base_2(i)) { 44 | total += i; 45 | // Py.print(i); 46 | } 47 | } 48 | Py.print(total); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/github/jabbalaci/pyjava/examples/Example10.java: -------------------------------------------------------------------------------- 1 | package com.github.jabbalaci.pyjava.examples; 2 | 3 | import java.util.List; 4 | 5 | /* 6 | removing duplicates 7 | 8 | Consider the following list: [5, 2, 3, 5, 1, 4, -200, 5, 1, 3, 2, 2, 5] . 9 | Remove the duplicates, i.e. an element should be present in the list maximum once. 10 | Let the result be a sorted list. 11 | */ 12 | 13 | import com.github.jabbalaci.pyjava.Py; 14 | 15 | public class Example10 { 16 | 17 | public static void main(String[] args) { 18 | Example10 m = new Example10(); 19 | m.start(); 20 | } 21 | 22 | private void start() { 23 | List li = Py.as_list(5, 2, 3, 5, 1, 4, -200, 5, 1, 3, 2, 2, 5); 24 | Py.print(li); 25 | Py.print(Py.sorted(Py.to_list(Py.to_set(li)))); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/jabbalaci/pyjava/proba/TestPy.java: -------------------------------------------------------------------------------- 1 | package com.github.jabbalaci.pyjava.proba; 2 | 3 | import java.io.ByteArrayOutputStream; 4 | import java.io.PrintStream; 5 | import java.util.HashSet; 6 | import java.util.List; 7 | import java.util.Set; 8 | 9 | import com.github.jabbalaci.pyjava.Py; 10 | import com.github.jabbalaci.pyjava.PyList; 11 | import com.github.jabbalaci.pyjava.PyStr; 12 | 13 | /** 14 | * This file is used as a sandbox for trying out different things. 15 | * 16 | * @author jabba 17 | */ 18 | 19 | public class TestPy { 20 | 21 | public static void main(String[] args) { 22 | TestPy m = new TestPy(); 23 | // m.built_in_functions(); 24 | // m.range(); 25 | // m.print(); 26 | // m.min_max_sum_prod(); 27 | // m.chr_ord(); 28 | // m.sort(); 29 | // m.print(); 30 | // m.array(); 31 | // m.bin_hex(); 32 | // m.conversions(); 33 | // m.stdout(); 34 | // m.sysout(); 35 | // m.set(); 36 | // m.py_dot_str(); 37 | // m.proba(); 38 | // m.input(); 39 | m.file_read(); 40 | } 41 | 42 | private void file_read() { 43 | String fpath = "/home/jabba/Dropbox/eclipse_ws/PyJava/src/test/resources/symbols.txt"; 44 | List lines = Py.readlines(fpath, "iso-8859-1"); 45 | Py.print(lines); 46 | Py.sep("-", 30); 47 | System.out.println("'" + Py.read(fpath) + "'"); 48 | } 49 | 50 | private void input() { 51 | String s = Py.input("Line: "); 52 | System.out.println(s); 53 | } 54 | 55 | private void proba() { 56 | // List li = Py.to_list(1, 2, 3); 57 | List li = Py.to_list("laci"); 58 | System.out.println(li); 59 | } 60 | 61 | private void py_dot_str() { 62 | Py.print(Py.str.reverse("PyJava")); 63 | } 64 | 65 | @SuppressWarnings("unused") 66 | private void set() { 67 | Set bag = new HashSet<>(); 68 | bag.add(7); bag.add(4); bag.add(9); bag.add(-200); 69 | List li = Py.sorted(Py.to_list(bag)); 70 | Py.print(bag); 71 | Py.print(li); 72 | 73 | Set uj = Py.to_set(li); 74 | Py.print(uj); 75 | } 76 | 77 | @SuppressWarnings("unused") 78 | private void sysout() { 79 | System.out.println(); 80 | } 81 | 82 | @SuppressWarnings("unused") 83 | private void stdout() { 84 | // Create a stream to hold the output 85 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 86 | PrintStream ps = new PrintStream(baos); 87 | // IMPORTANT: Save the old System.out! 88 | PrintStream old = System.out; 89 | // Tell Java to use your special stream 90 | System.setOut(ps); 91 | // Print some output: goes to your special stream 92 | System.out.println("Foofoofoo!"); 93 | // Put things back 94 | System.out.flush(); 95 | System.setOut(old); 96 | // Show what happened 97 | System.out.println("Here: " + baos.toString()); 98 | } 99 | 100 | @SuppressWarnings("unused") 101 | private void conversions() { 102 | Py.print(Py.to_int("42f")); 103 | 104 | } 105 | 106 | @SuppressWarnings("unused") 107 | private void bin_hex() { 108 | int n = 585; 109 | Py.print(Py.bin(n)); 110 | Py.print(Py.bin(n, false)); 111 | } 112 | 113 | // @SuppressWarnings("unused") 114 | // private void array() { 115 | // Integer[] eh = Py.to_int_array(1, 2, 3, 4, 5); 116 | // Py.print(eh.length); 117 | // } 118 | 119 | @SuppressWarnings("unused") 120 | private void sort() { 121 | List li = Py.new_list(0); 122 | li.add(3); li.add(1); li.add(6); li.add(5); 123 | Py.print(li); 124 | 125 | List copy = Py.sorted(li); 126 | Py.print(copy); 127 | Py.print(li); 128 | Py.sep("-", 30); 129 | PyList.sort(li); 130 | Py.print(li); 131 | } 132 | 133 | @SuppressWarnings("unused") 134 | private void chr_ord() { 135 | Py.print(Py.chr(65)); 136 | Py.print(Py.ord('A')); 137 | Py.print(Py.ord("A")); 138 | } 139 | 140 | @SuppressWarnings("unused") 141 | private void min_max_sum_prod() { 142 | // List li = Py.list(0); 143 | // li.add(3); 144 | // Py.print(li); 145 | // // 146 | // List li2 = Py.list(0.0); 147 | // li2.add(3.14); 148 | // Py.print(li2); 149 | // // 150 | // List li3 = Py.list(""); 151 | // li3.add("PyJava"); 152 | // Py.print(li3); 153 | 154 | List li = Py.new_list(0); 155 | li.add(3); li.add(1); li.add(6); li.add(5); 156 | Py.print(li); 157 | Py.print(Py.max(li)); 158 | Py.print(Py.min(li)); 159 | Py.print(Py.sum(li, 0)); 160 | Py.print(Py.prod(li, 0)); 161 | // 162 | List li2 = Py.new_list(0); 163 | Py.print(Py.max(li2)); 164 | int maxi = Py.max(li2); 165 | } 166 | 167 | @SuppressWarnings("unused") 168 | private void print() { 169 | Py.print("Hello, PyJava!", ""); 170 | Py.print("second line"); 171 | } 172 | 173 | @SuppressWarnings("unused") 174 | private void range() { 175 | List li = Py.range2(5); 176 | Py.print(li); 177 | // 178 | List li6 = Py.range2(0); 179 | Py.print(li6); 180 | // 181 | List li7 = Py.range2(-2); 182 | Py.print(li7); 183 | // 184 | List li2 = Py.range2(5, 15); 185 | Py.print(li2); 186 | // 187 | List li8 = Py.range2(15, 5); 188 | Py.print(li8); 189 | // 190 | List li3 = Py.range2(5, 15, 2); 191 | Py.print(li3); 192 | // 193 | List li9 = Py.range2(10, 4, 3); 194 | Py.print(li9); 195 | // 196 | List li4 = Py.range2(15, 5, -1); 197 | Py.print(li4); 198 | // 199 | // List li5 = Py.range(15, 5, 0); 200 | // Py.print(li5); 201 | 202 | } 203 | 204 | @SuppressWarnings("unused") 205 | private void built_in_functions() { 206 | String s = Py.to_str(6); 207 | Py.print(s); 208 | // 209 | String s2 = Py.to_str(3.14); 210 | Py.print(s2); 211 | // 212 | int n = Py.to_int("42"); 213 | Py.print(n); 214 | // 215 | double d = Py.to_double("3.14"); 216 | Py.print(d); 217 | // 218 | float f = Py.to_float("3.14"); 219 | Py.print(f); 220 | // 221 | // String line = Py.input(); 222 | // Py.print(line); 223 | // String line = Py.input("Name: "); 224 | // Py.print(line); 225 | // 226 | String s3 = "jabba"; 227 | Py.print(PyStr.capitalize(s3)); 228 | // 229 | String s4 = "Pythonization of Java"; 230 | Py.print(PyStr.reverse(s4)); 231 | } 232 | 233 | } 234 | -------------------------------------------------------------------------------- /src/test/java/com/github/jabbalaci/pyjava/AsciiTest.java: -------------------------------------------------------------------------------- 1 | package com.github.jabbalaci.pyjava; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.Test; 6 | 7 | import com.github.jabbalaci.pyjava.Py; 8 | import com.github.jabbalaci.pyjava.TypeError; 9 | import com.github.jabbalaci.pyjava.ValueError; 10 | 11 | public class AsciiTest { 12 | 13 | @Test 14 | public void testChr() { 15 | assertEquals("A", Py.chr(65)); 16 | assertEquals("d", Py.chr(100)); 17 | } 18 | 19 | @Test(expected = ValueError.class) 20 | public void testChrException1() { 21 | assertEquals("_", Py.chr(256)); // _ for dummy value 22 | } 23 | 24 | @Test(expected = ValueError.class) 25 | public void testChrException2() { 26 | assertEquals("_", Py.chr(-1)); // _ for dummy value 27 | } 28 | 29 | @Test 30 | public void testOrdChar() { 31 | assertEquals(97, Py.ord('a')); 32 | } 33 | 34 | @Test 35 | public void testOrdString() { 36 | assertEquals(97, Py.ord("a")); 37 | } 38 | 39 | @Test(expected = TypeError.class) 40 | public void testOrdStringException1() { 41 | assertEquals(0, Py.ord("")); // 0 for dummy value 42 | } 43 | 44 | @Test(expected = TypeError.class) 45 | public void testOrdStringException2() { 46 | assertEquals(0, Py.ord("aa")); // 0 for dummy value 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/test/java/com/github/jabbalaci/pyjava/ConversionsTest.java: -------------------------------------------------------------------------------- 1 | package com.github.jabbalaci.pyjava; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.Test; 6 | 7 | import com.github.jabbalaci.pyjava.Common; 8 | import com.github.jabbalaci.pyjava.Py; 9 | 10 | public class ConversionsTest { 11 | 12 | @Test 13 | public void testStrInt() { 14 | assertEquals("6", Py.to_str(6)); 15 | assertEquals("0", Py.to_str(0)); 16 | assertEquals("-20", Py.to_str(-20)); 17 | } 18 | 19 | @Test 20 | public void testStrDouble() { 21 | assertEquals("6.0", Py.to_str(6.0)); 22 | assertEquals("0.0", Py.to_str(0.0)); 23 | assertEquals("-6.0", Py.to_str(-6.0)); 24 | } 25 | 26 | @Test 27 | public void testTo_int() { 28 | assertEquals(42, Py.to_int("42")); 29 | assertEquals(0, Py.to_int("0")); 30 | assertEquals(-42, Py.to_int("-42")); 31 | } 32 | 33 | @Test 34 | public void testTo_double() { 35 | assertEquals(3.14, Py.to_double("3.14"), Common.TOLERANCE); 36 | assertEquals(0.0, Py.to_double("0.0"), Common.TOLERANCE); 37 | assertEquals(-3.14, Py.to_double("-3.14"), Common.TOLERANCE); 38 | } 39 | 40 | @Test 41 | public void testTo_float() { 42 | assertEquals(3.14, Py.to_float("3.14"), Common.TOLERANCE); 43 | assertEquals(0.0, Py.to_float("0.0"), Common.TOLERANCE); 44 | assertEquals(-3.14, Py.to_float("-3.14"), Common.TOLERANCE); 45 | } 46 | 47 | // ---------- bin / hex ---------- 48 | 49 | @Test 50 | public void testBin() { 51 | assertEquals("0b0", Py.bin(0)); 52 | assertEquals("0b1010", Py.bin(10)); 53 | assertEquals("-0b1010", Py.bin(-10)); 54 | } 55 | 56 | @Test 57 | public void testBin_with_prefix() { 58 | assertEquals("0b0", Py.bin(0, true)); 59 | assertEquals("0b1010", Py.bin(10, true)); 60 | assertEquals("-0b1010", Py.bin(-10, true)); 61 | } 62 | 63 | @Test 64 | public void testBin_without_prefix() { 65 | assertEquals("0", Py.bin(0, false)); 66 | assertEquals("1010", Py.bin(10, false)); 67 | assertEquals("-1010", Py.bin(-10, false)); 68 | } 69 | 70 | 71 | } 72 | -------------------------------------------------------------------------------- /src/test/java/com/github/jabbalaci/pyjava/FileReadTest.java: -------------------------------------------------------------------------------- 1 | package com.github.jabbalaci.pyjava; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import java.io.File; 6 | import java.io.IOError; 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | 10 | import org.junit.Test; 11 | 12 | import com.github.jabbalaci.pyjava.Py; 13 | import com.github.jabbalaci.pyjava.TypeError; 14 | import com.github.jabbalaci.pyjava.ValueError; 15 | 16 | public class FileReadTest { 17 | 18 | /** 19 | * http://stackoverflow.com/a/28674230/232485 20 | * 21 | * @param fname File name of a resource file. 22 | * @return Absolute path of the given resource file. 23 | */ 24 | private String getPathOfResourceFile(String fname) { 25 | ClassLoader classLoader = getClass().getClassLoader(); 26 | File file = new File(classLoader.getResource(fname).getFile()); 27 | return file.getAbsolutePath(); 28 | } 29 | 30 | @Test 31 | public void testReadlines_1() { 32 | String fpath = getPathOfResourceFile("text.txt"); 33 | List got = Py.readlines(fpath); 34 | List expected = new ArrayList<>(); 35 | expected.add("First line."); 36 | expected.add("Second line."); 37 | assertEquals(expected, got); 38 | } 39 | 40 | @Test 41 | public void testReadlines_2() { 42 | String fpath = getPathOfResourceFile("symbols.txt"); 43 | List got = Py.readlines(fpath, "iso-8859-1"); 44 | } 45 | 46 | @Test 47 | public void testRead_1() { 48 | String fpath = getPathOfResourceFile("text.txt"); 49 | String got = Py.read(fpath); 50 | String expected = "First line.\nSecond line.\n"; 51 | assertEquals(expected, got); 52 | } 53 | 54 | @Test 55 | public void testRead_2() { 56 | String fpath = getPathOfResourceFile("text.txt"); 57 | String got = Py.read(fpath, "iso-8859-1"); 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/test/java/com/github/jabbalaci/pyjava/ListTest.java: -------------------------------------------------------------------------------- 1 | package com.github.jabbalaci.pyjava; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | import org.junit.Test; 9 | 10 | import com.github.jabbalaci.pyjava.Common; 11 | import com.github.jabbalaci.pyjava.Py; 12 | import com.github.jabbalaci.pyjava.PyList; 13 | import com.github.jabbalaci.pyjava.ValueError; 14 | 15 | public class ListTest { 16 | 17 | @Test 18 | public void testCreate_new_list() { 19 | List a = Py.new_list(0); 20 | assertEquals(0, a.size()); 21 | List b = Py.new_list(0.0); 22 | assertEquals(0, b.size()); 23 | List c = Py.new_list(""); 24 | assertEquals(0, c.size()); 25 | // 26 | List d = Py.to_list(""); // another way to create an empty string list 27 | assertEquals(0, d.size()); 28 | } 29 | 30 | @Test 31 | public void testAs_list() { 32 | List li = Py.as_list(1, 2, 3); 33 | assertEquals(3, li.size()); 34 | assertEquals(1, (int)li.get(0)); 35 | assertEquals(2, (int)li.get(1)); 36 | assertEquals(3, (int)li.get(2)); 37 | // 38 | List b = new ArrayList<>(); 39 | b.add(1); b.add(2); b.add(3); 40 | assertEquals(true, li.equals(b)); 41 | // 42 | li.add(9); 43 | assertEquals(4, li.size()); 44 | assertEquals(9, (int)li.get(3)); 45 | } 46 | 47 | @Test 48 | public void testLists_equal_int() { 49 | List l1 = Py.as_list(1, 2, 3); 50 | List l2 = Py.as_list(1, 2, 3); 51 | assertEquals(true, l1.equals(l2)); 52 | // 53 | l2.add(4); 54 | assertEquals(false, l1.equals(l2)); 55 | // 56 | l1.clear(); l2.clear(); 57 | assertEquals(true, l1.equals(l2)); 58 | // 59 | l1.add(1); 60 | assertEquals(false, l1.equals(l2)); 61 | // 62 | l2.add(1); 63 | assertEquals(true, l1.equals(l2)); 64 | // 65 | l1.clear(); l2.clear(); 66 | l1.add(1); l1.add(2); 67 | l2.add(100); l2.add(2); 68 | assertEquals(false, l1.equals(l2)); 69 | } 70 | 71 | @Test 72 | public void testLists_equal_double() { 73 | List l1 = Py.as_list(1.0, 2.0, 3.0); 74 | List l2 = Py.as_list(1.0, 2.0, 3.0); 75 | assertEquals(true, l1.equals(l2)); 76 | // 77 | l2.add(4.0); 78 | assertEquals(false, l1.equals(l2)); 79 | // 80 | l1.clear(); l2.clear(); 81 | assertEquals(true, l1.equals(l2)); 82 | // 83 | l1.add(1.0); 84 | assertEquals(false, l1.equals(l2)); 85 | // 86 | l2.add(1.0); 87 | assertEquals(true, l1.equals(l2)); 88 | // 89 | l1.clear(); l2.clear(); 90 | l1.add(1.0); l1.add(2.0); 91 | l2.add(1.001); l2.add(2.0); 92 | assertEquals(false, l1.equals(l2)); // out of tolerance 93 | } 94 | 95 | @Test 96 | public void testLists_equal_String() { 97 | List l1 = Py.as_list("a", "b", "c"); 98 | List l2 = Py.as_list("a", "b", "c"); 99 | assertEquals(true, l1.equals(l2)); 100 | 101 | l2.add("d"); 102 | assertEquals(false, l1.equals(l2)); 103 | // 104 | l1.clear(); l2.clear(); 105 | assertEquals(true, l1.equals(l2)); 106 | // 107 | l1.add("a"); 108 | assertEquals(false, l1.equals(l2)); 109 | // 110 | l2.add("a"); 111 | assertEquals(true, l1.equals(l2)); 112 | // 113 | l1.clear(); l2.clear(); 114 | l1.add("aa"); l1.add("bb"); 115 | l2.add("AA"); l2.add("bb"); 116 | assertEquals(false, l1.equals(l2)); 117 | } 118 | 119 | @Test 120 | public void testList() { 121 | List l1 = new ArrayList<>(); 122 | List l2 = Py.new_list(0); 123 | assertEquals(true, l1.equals(l2)); 124 | // 125 | List l1_d = new ArrayList<>(); 126 | List l2_d = Py.new_list(0.0); 127 | assertEquals(true, l1_d.equals(l2_d)); 128 | // 129 | List l1_s = new ArrayList<>(); 130 | List l2_s = Py.to_list(""); 131 | assertEquals(true, l1_s.equals(l2_s)); 132 | } 133 | 134 | @Test 135 | public void testMax() { 136 | List li = Py.as_list(3, 1, 6, 5); 137 | assertEquals(6, (int)Py.max(li)); 138 | } 139 | 140 | @Test 141 | public void testMin() { 142 | List li = Py.as_list(3, 1, 6, 5); 143 | assertEquals(1, (int)Py.min(li)); 144 | } 145 | 146 | @Test 147 | public void testMax_double() { 148 | List li = Py.as_list(3.0, 1.0, 6.0, 5.0); 149 | assertEquals(6.0, (double)Py.max(li), Common.TOLERANCE); 150 | } 151 | 152 | @Test 153 | public void testMin_double() { 154 | List li = Py.as_list(3.0, 1.0, 6.0, 5.0); 155 | li.add(3.0); li.add(1.0); li.add(6.0); li.add(5.0); 156 | assertEquals(1.0, (double)Py.min(li), Common.TOLERANCE); 157 | } 158 | 159 | @Test 160 | public void testMax_String() { 161 | List li = Py.as_list("c", "a", "f", "e"); 162 | assertEquals("f", (String)Py.max(li)); 163 | } 164 | 165 | @Test 166 | public void testMin_String() { 167 | List li = Py.as_list("c", "a", "f", "e"); 168 | assertEquals("a", (String)Py.min(li)); 169 | } 170 | 171 | @Test(expected = ValueError.class) 172 | public void testMaxException() { 173 | List li = Py.new_list(0); 174 | assertEquals(0, (int)Py.max(li)); // dummy value 175 | } 176 | 177 | @Test(expected = ValueError.class) 178 | public void testMinException() { 179 | List li = Py.new_list(0); 180 | assertEquals(0, (int)Py.min(li)); // dummy value 181 | } 182 | 183 | @Test 184 | public void testSum() { 185 | List li = Py.as_list(3, 1, 6, 5); 186 | assertEquals(15, Py.sum(li, 0)); 187 | // 188 | li.clear(); 189 | assertEquals(0, Py.sum(li, 0)); 190 | } 191 | 192 | @Test 193 | public void testSum_double() { 194 | List li = Py.as_list(3.0, 1.0, 6.0, 5.0); 195 | assertEquals(15.0, Py.sum(li, 0), Common.TOLERANCE); 196 | // 197 | li.clear(); 198 | assertEquals(0.0, Py.sum(li, 0.0), Common.TOLERANCE); 199 | } 200 | 201 | @Test 202 | public void testProd() { 203 | List li = Py.as_list(3, 1, 6, 5); 204 | assertEquals(90, Py.prod(li, 0)); 205 | // 206 | li.clear(); 207 | assertEquals(1, Py.prod(li, 0)); 208 | } 209 | 210 | @Test 211 | public void testProd_double() { 212 | List li = Py.as_list(3.0, 1.0, 6.0, 5.0); 213 | assertEquals(90.0, Py.prod(li, 0), Common.TOLERANCE); 214 | // 215 | li.clear(); 216 | assertEquals(1.0, Py.prod(li, 0.0), Common.TOLERANCE); 217 | } 218 | 219 | // ---------- sort* ---------- 220 | 221 | @Test 222 | public void testSorted() { 223 | List unsorted = Py.as_list(3, 1, 6, 5); 224 | // 225 | List work = Py.as_list(3, 1, 6, 5); 226 | // 227 | List sorted = Py.as_list(1, 3, 5, 6); 228 | // 229 | List result = Py.sorted(work); 230 | // work is unchanged 231 | assertEquals(true, work.equals(unsorted)); 232 | // result is sorted 233 | assertEquals(true, result.equals(sorted)); 234 | // empty list // 235 | List empty = Py.new_list(0); 236 | result = Py.sorted(empty); 237 | assertEquals(true, result.isEmpty()); 238 | } 239 | 240 | @Test 241 | public void testSorted_reverse_false() { 242 | List unsorted = Py.as_list(3, 1, 6, 5); 243 | // 244 | List work = Py.as_list(3, 1, 6, 5); 245 | // 246 | List sorted = Py.as_list(1, 3, 5, 6); 247 | // 248 | List result = Py.sorted(work, false); 249 | // work is unchanged 250 | assertEquals(true, work.equals(unsorted)); 251 | // result is sorted 252 | assertEquals(true, result.equals(sorted)); 253 | // empty list // 254 | List empty = Py.new_list(0); 255 | result = Py.sorted(empty, false); 256 | assertEquals(true, result.isEmpty()); 257 | } 258 | 259 | @Test 260 | public void testSorted_reverse_true() { 261 | List unsorted = Py.as_list(3, 1, 6, 5); 262 | // 263 | List work = Py.as_list(3, 1, 6, 5); 264 | // 265 | List reversed = Py.as_list(6, 5, 3, 1); 266 | // 267 | List result = Py.sorted(work, true); // decreasing order 268 | // work is unchanged 269 | assertEquals(true, work.equals(unsorted)); 270 | // result is sorted in decreasing order 271 | assertEquals(true, result.equals(reversed)); 272 | // empty list // 273 | List empty = Py.new_list(0); 274 | result = Py.sorted(empty, true); 275 | assertEquals(true, result.isEmpty()); 276 | } 277 | 278 | @Test 279 | public void testSort() { 280 | List unsorted = Py.as_list(3, 1, 6, 5); 281 | // 282 | List work = Py.as_list(3, 1, 6, 5); 283 | // 284 | List sorted = Py.as_list(1, 3, 5, 6); 285 | // 286 | PyList.sort(work); 287 | // work is changed 288 | assertEquals(false, work.equals(unsorted)); 289 | // work is sorted 290 | assertEquals(true, work.equals(sorted)); 291 | // empty list // 292 | List empty = Py.new_list(0); 293 | PyList.sort(empty); 294 | assertEquals(true, empty.isEmpty()); 295 | } 296 | 297 | @Test 298 | public void testSort_reverse_false() { 299 | List unsorted = Py.as_list(3, 1, 6, 5); 300 | // 301 | List work = Py.as_list(3, 1, 6, 5); 302 | // 303 | List sorted = Py.as_list(1, 3, 5, 6); 304 | // 305 | PyList.sort(work, false); 306 | // work is changed 307 | assertEquals(false, work.equals(unsorted)); 308 | // work is sorted 309 | assertEquals(true, work.equals(sorted)); 310 | // empty list // 311 | List empty = Py.new_list(0); 312 | PyList.sort(empty, false); 313 | assertEquals(true, empty.isEmpty()); 314 | } 315 | 316 | @Test 317 | public void testSort_reverse_true() { 318 | List unsorted = Py.as_list(3, 1, 6, 5); 319 | // 320 | List work = Py.as_list(3, 1, 6, 5); 321 | // 322 | List reversed = Py.as_list(6, 5, 3, 1); 323 | // 324 | PyList.sort(work, true); // decreasing order 325 | // work is changed 326 | assertEquals(false, work.equals(unsorted)); 327 | // work is sorted 328 | assertEquals(true, work.equals(reversed)); 329 | // empty list // 330 | List empty = Py.new_list(0); 331 | PyList.sort(empty, true); 332 | assertEquals(true, empty.isEmpty()); 333 | } 334 | 335 | // ---------- reverse* ---------- 336 | 337 | @Test 338 | public void testReversed() { 339 | List work = Py.as_list(1, 3, 5, 6); 340 | // 341 | List sorted = Py.as_list(1, 3, 5, 6); 342 | // 343 | List reversed = Py.as_list(6, 5, 3, 1); 344 | // 345 | List result = Py.reversed(work); 346 | // work is unchanged 347 | assertEquals(true, work.equals(sorted)); 348 | // result is reversed 349 | assertEquals(true, result.equals(reversed)); 350 | // empty list // 351 | List empty = Py.new_list(0); 352 | result = Py.reversed(empty); 353 | assertEquals(true, result.isEmpty()); 354 | } 355 | 356 | @Test 357 | public void testReverse() { 358 | List work = Py.as_list(1, 3, 5, 6); 359 | // 360 | List sorted = Py.as_list(1, 3, 5, 6); 361 | // 362 | List reversed = Py.as_list(6, 5, 3, 1); 363 | // 364 | PyList.reverse(work); 365 | // work is changed 366 | assertEquals(false, work.equals(sorted)); 367 | // work is reversed 368 | assertEquals(true, work.equals(reversed)); 369 | // empty list // 370 | List empty = Py.new_list(0); 371 | PyList.reverse(empty); 372 | assertEquals(true, empty.isEmpty()); 373 | } 374 | 375 | // ---------- list(...) ---------- 376 | 377 | @Test 378 | public void testList_explode_string() { 379 | List li = Py.to_list("python"); 380 | assertEquals(6, li.size()); 381 | assertEquals("p", li.get(0)); 382 | assertEquals("y", li.get(1)); 383 | assertEquals("t", li.get(2)); 384 | assertEquals("h", li.get(3)); 385 | assertEquals("o", li.get(4)); 386 | assertEquals("n", li.get(5)); 387 | // 388 | li = Py.to_list(""); 389 | assertEquals(true, li.isEmpty()); 390 | } 391 | 392 | } -------------------------------------------------------------------------------- /src/test/java/com/github/jabbalaci/pyjava/RangeTest.java: -------------------------------------------------------------------------------- 1 | package com.github.jabbalaci.pyjava; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import java.util.List; 6 | 7 | import org.junit.Test; 8 | 9 | import com.github.jabbalaci.pyjava.Py; 10 | import com.github.jabbalaci.pyjava.ValueError; 11 | 12 | public class RangeTest { 13 | 14 | @Test 15 | public void testRange2_end() { 16 | List a = Py.range2(5); 17 | assertEquals(5, a.size()); 18 | assertEquals(0, (int)a.get(0)); 19 | assertEquals(1, (int)a.get(1)); 20 | assertEquals(2, (int)a.get(2)); 21 | assertEquals(3, (int)a.get(3)); 22 | assertEquals(4, (int)a.get(4)); 23 | // 24 | a = Py.range2(0); 25 | assertEquals(true, a.isEmpty()); 26 | // 27 | a = Py.range2(-1); 28 | assertEquals(true, a.isEmpty()); 29 | // 30 | a = Py.range2(1000); 31 | assertEquals(1000, a.size()); 32 | } 33 | 34 | @Test 35 | public void testRange2_start_end() { 36 | List a = Py.range2(3, 6); 37 | assertEquals(3, a.size()); 38 | assertEquals(3, (int)a.get(0)); 39 | assertEquals(4, (int)a.get(1)); 40 | assertEquals(5, (int)a.get(2)); 41 | // 42 | a = Py.range2(6, 3); 43 | assertEquals(true, a.isEmpty()); 44 | // 45 | a = Py.range2(6, 6); 46 | assertEquals(true, a.isEmpty()); 47 | } 48 | 49 | @Test 50 | public void testRange2_start_end_positiveStep() { 51 | List a = Py.range2(3, 10, 2); 52 | assertEquals(4, a.size()); 53 | assertEquals(3, (int)a.get(0)); 54 | assertEquals(5, (int)a.get(1)); 55 | assertEquals(7, (int)a.get(2)); 56 | assertEquals(9, (int)a.get(3)); 57 | // 58 | a = Py.range2(6, 3, 2); 59 | assertEquals(true, a.isEmpty()); 60 | // 61 | a = Py.range2(5, 5, 3); 62 | assertEquals(true, a.isEmpty()); 63 | } 64 | 65 | @Test 66 | public void testRange2_start_end_negativeStep() { 67 | List a = Py.range2(10, 5, -1); 68 | assertEquals(5, a.size()); 69 | assertEquals(10, (int)a.get(0)); 70 | assertEquals(9, (int)a.get(1)); 71 | assertEquals(8, (int)a.get(2)); 72 | assertEquals(7, (int)a.get(3)); 73 | assertEquals(6, (int)a.get(4)); 74 | // 75 | a = Py.range2(5, 10, -1); 76 | assertEquals(true, a.isEmpty()); 77 | // 78 | a = Py.range2(5, 5, -1); 79 | assertEquals(true, a.isEmpty()); 80 | } 81 | 82 | @Test(expected = ValueError.class) 83 | public void testRange2_start_end_zeroStep() { 84 | @SuppressWarnings("unused") 85 | List dummy = Py.range2(10, 5, 0); 86 | } 87 | 88 | } -------------------------------------------------------------------------------- /src/test/java/com/github/jabbalaci/pyjava/SetTest.java: -------------------------------------------------------------------------------- 1 | package com.github.jabbalaci.pyjava; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import java.util.List; 6 | import java.util.Set; 7 | 8 | import org.junit.Test; 9 | 10 | public class SetTest { 11 | 12 | @Test 13 | public void testList_to_set() { 14 | List li = Py.as_list(8, 5, 2, 4); 15 | Set bag = Py.to_set(li); 16 | assertEquals(4, bag.size()); 17 | assertEquals(true, bag.contains(8)); 18 | assertEquals(true, bag.contains(5)); 19 | assertEquals(true, bag.contains(2)); 20 | assertEquals(true, bag.contains(4)); 21 | // 22 | li.add(8); 23 | li.add(5); 24 | assertEquals(6, li.size()); 25 | Set nodup = Py.to_set(li); 26 | assertEquals(4, nodup.size()); 27 | } 28 | 29 | @Test 30 | public void testSet_to_list() { 31 | List li = Py.as_list(8, 5, 2, 4, 8, 5); 32 | assertEquals(6, li.size()); 33 | Set bag = Py.to_set(li); 34 | assertEquals(4, bag.size()); 35 | // 36 | List res = Py.to_list(bag); 37 | assertEquals(4, res.size()); 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /src/test/java/com/github/jabbalaci/pyjava/StdoutTest.java: -------------------------------------------------------------------------------- 1 | package com.github.jabbalaci.pyjava; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import java.io.ByteArrayOutputStream; 6 | import java.io.PrintStream; 7 | 8 | import org.junit.After; 9 | import org.junit.Before; 10 | import org.junit.Test; 11 | 12 | import com.github.jabbalaci.pyjava.Py; 13 | 14 | public class StdoutTest { 15 | 16 | private ByteArrayOutputStream baos; 17 | 18 | private PrintStream old; 19 | 20 | @Before 21 | public void stdoutRedirectionOn() { 22 | this.baos = new ByteArrayOutputStream(); 23 | PrintStream ps = new PrintStream(baos); 24 | this.old = System.out; 25 | System.setOut(ps); 26 | } 27 | 28 | @After 29 | public void stdoutRedirectionOff() { 30 | System.setOut(this.old); 31 | } 32 | 33 | private String getStdout() { 34 | System.out.flush(); 35 | return baos.toString().replace("\r\n", "\n"); 36 | } 37 | 38 | @Test 39 | public void testPrint() { 40 | Py.print(""); 41 | assertEquals("\n", this.getStdout()); 42 | } 43 | 44 | @Test 45 | public void testPrint_nude() { 46 | Py.print(); 47 | assertEquals("\n", this.getStdout()); 48 | } 49 | 50 | @Test 51 | public void testPrint_2() { 52 | Py.print("hello"); 53 | assertEquals("hello\n", this.getStdout()); 54 | } 55 | 56 | @Test 57 | public void testPrint_with_end() { 58 | Py.print("aa", ""); 59 | assertEquals("aa", this.getStdout()); 60 | } 61 | 62 | @Test 63 | public void testPrint_with_end_2() { 64 | Py.print("aa", " "); 65 | assertEquals("aa ", this.getStdout()); 66 | } 67 | 68 | @Test 69 | public void testPrint_with_end_3() { 70 | Py.print("aa", "::"); 71 | assertEquals("aa::", this.getStdout()); 72 | } 73 | 74 | @Test 75 | public void testPrint_with_end_4() { 76 | Py.print("aa", "\n"); 77 | assertEquals("aa\n", this.getStdout()); 78 | } 79 | 80 | @Test 81 | public void testPrint_with_end_5() { 82 | Py.print("aa", "::\n"); 83 | assertEquals("aa::\n", this.getStdout()); 84 | } 85 | 86 | @Test 87 | public void testPrintf() { 88 | Py.printf("%s", "py"); 89 | assertEquals("py", this.getStdout()); 90 | } 91 | 92 | @Test 93 | public void testPrintf_2() { 94 | Py.printf("%s was released in %d", "Python", 1991); 95 | assertEquals("Python was released in 1991", this.getStdout()); 96 | } 97 | 98 | @Test 99 | public void testPrintf_3() { 100 | Py.printf("%s\n", "py"); 101 | assertEquals("py\n", this.getStdout()); 102 | } 103 | 104 | @Test 105 | public void testSep() { 106 | Py.sep("-", 3); 107 | assertEquals("---\n", this.getStdout()); 108 | } 109 | 110 | @Test 111 | public void testSep_2() { 112 | Py.sep("ab", 3); 113 | assertEquals("ababab\n", this.getStdout()); 114 | } 115 | 116 | @Test 117 | public void testSep_3() { 118 | Py.sep("ab", 2); 119 | Py.sep("ab", 2); 120 | assertEquals("abab\nabab\n", this.getStdout()); 121 | } 122 | 123 | } 124 | -------------------------------------------------------------------------------- /src/test/java/com/github/jabbalaci/pyjava/StringMethodsTest.java: -------------------------------------------------------------------------------- 1 | package com.github.jabbalaci.pyjava; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import java.util.Arrays; 6 | import java.util.List; 7 | 8 | import org.junit.Test; 9 | 10 | import com.github.jabbalaci.pyjava.PyStr; 11 | 12 | public class StringMethodsTest { 13 | 14 | @Test 15 | public void testCapitalize() { 16 | assertEquals("", PyStr.capitalize("")); 17 | assertEquals("Jabba", PyStr.capitalize("jabba")); 18 | assertEquals("Jabba", PyStr.capitalize("jABBA")); 19 | assertEquals("Jabba", PyStr.capitalize("JABBA")); 20 | } 21 | 22 | @Test 23 | public void testReverse() { 24 | assertEquals("", PyStr.reverse("")); 25 | assertEquals("a", PyStr.reverse("a")); 26 | assertEquals("ba", PyStr.reverse("ab")); 27 | assertEquals("avaJyP", PyStr.reverse("PyJava")); 28 | } 29 | 30 | @Test 31 | public void testRepeat() { 32 | assertEquals("", PyStr.repeat("-", 0)); 33 | assertEquals("", PyStr.repeat("-", -1)); 34 | assertEquals("-", PyStr.repeat("-", 1)); 35 | assertEquals("-----", PyStr.repeat("-", 5)); 36 | } 37 | 38 | @Test 39 | public void testCenter() { 40 | assertEquals("*", PyStr.center("*", 1)); 41 | assertEquals(" * ", PyStr.center("*", 3)); 42 | assertEquals(" * ", PyStr.center("*", 4)); 43 | assertEquals(" ** ", PyStr.center("**", 4)); 44 | // 45 | assertEquals("*", PyStr.center("*", 0)); 46 | assertEquals("*****", PyStr.center("*****", 3)); 47 | } 48 | 49 | @Test 50 | public void testSplit() { 51 | List li = PyStr.split("a:b:c:d", ":"); 52 | assertEquals(4, li.size()); 53 | assertEquals("a", li.get(0)); 54 | assertEquals("b", li.get(1)); 55 | assertEquals("c", li.get(2)); 56 | assertEquals("d", li.get(3)); 57 | // 58 | li = PyStr.split("a:b:c:d", "x"); 59 | assertEquals(1, li.size()); 60 | assertEquals("a:b:c:d", li.get(0)); 61 | } 62 | 63 | @Test 64 | public void testSplit_no_sep() { 65 | List li = PyStr.split(" a b c d "); 66 | assertEquals(4, li.size()); 67 | assertEquals("a", li.get(0)); 68 | assertEquals("b", li.get(1)); 69 | assertEquals("c", li.get(2)); 70 | assertEquals("d", li.get(3)); 71 | // 72 | li = PyStr.split(" "); 73 | assertEquals(true, li.isEmpty()); 74 | } 75 | 76 | @Test 77 | public void testJoin() { 78 | List li = Arrays.asList("aa", "bb", "cc"); 79 | assertEquals("aa:bb:cc", PyStr.join(":", li)); 80 | assertEquals("aabbcc", PyStr.join("", li)); 81 | assertEquals("aa", PyStr.join(":", Arrays.asList("aa"))); 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /src/test/resources/symbols.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jabbalaci/PyJava/b805ea1d0e05f9675ff0eb720402dd3cfeb1e4f4/src/test/resources/symbols.txt -------------------------------------------------------------------------------- /src/test/resources/text.txt: -------------------------------------------------------------------------------- 1 | First line. 2 | Second line. 3 | --------------------------------------------------------------------------------