A user-friendly python programming environment for beginners.
The ideas of this environment are based on the great ideas from Schreib dein Programm!.
Write Your Python Program can be installed outside of Visual Studio Code via pip:
pip3 install wypp
After installation, you can use the wypp
command
for running your python files, making all features explained below available.
Run wypp --help
for usage information.
Here is the Changelog.
from wypp import *
.Here is a screen shot:
When hitting the RUN button, the vscode extension saves the current file, opens a terminal and executes the file with Python, staying in interactive mode after all definitions have been executed.
The file being executed should contain the following import statement in the first line:
from wypp import*
Running the file with the RUN button makes the following features available:
You can define enums, records and union data types and the use them with the type hints of Python 3. Type hints are checked for correctness dynamically, i.e. violations are detected only at the moment when a function is applied to an argument not matching its type hint or when a function returns a value not matching the return type hint. (This approach is similar to contract checking in racket)
type Color = Literal['red', 'green', 'blue']
@record
class Point:
x: float
y: float
@record
class Square:
center: Point
width: float
@record
class Circle:
center: Point
radius: float
You work with a record like this:
p = Point(2, 3) # point at x=2, y=3
print(p.x) # Prints 2
Fields of records are immutable by default. You get mutable fields with @record(mutable=True)
.
type PrimitiveShape = Union[Circle, Square]
To use recursive types, you need to write a forward reference to the yet undefined type as a string:
type Shape = Union[Circle, Square, 'Overlay']
@record
class Overlay:
top: Shape
bottom: Shape
Case distinction works like this:
def workOnShape(s: Shape) -> None:
if isinstance(s, Square):
# s is a Square, do something with it
pass
elif isinstance(s, Circle):
# s is a Circle, do something with it
pass
elif isinstance(s, Overlay):
# s is an Overlay, do something with it
pass
The body of workOnShape
can safely assume that s
is indeed one Square
, Circle
, or
Overlay
because the type hint Shape
for argument s
is checked dynamically. Here is
what happens if you apply workOnShape
to, say, a string, that is workOnShape('foo')
.
Traceback (most recent call last):
File "test.py", line 42, in <module>
workOnShape("foo")
WyppTypeError: got value of wrong type
given: 'foo'
expected: value of type Union[Circle, Square, Overlay]
context: workOnShape(s: Union[Circle, Square, Overlay]) -> None
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
declared at: test.py:31
caused by: test.py:42
| workOnShape("foo")
Tests are defined via check
. The first argument of check is the actual result,
then second argument the expected result.
check(factorial(4), 24)
All python types (builtin or from the typing
module) can be used as annotations.
Also, you can use every class as a type. In addition, WYPP comes with the following
predefined types:
floatNegative
floatNonNegative
floatNonPositive
floatPositive
intNegative
intNonNegative
intNonPositive
intPositive
nat
The code is run with from __future__ import annotations
(see PEP 563).
This means that you can use a type as an annotation
before the type being defined, for example to define recursive types or as
the type of self
inside of classes. In fact, there is no check at all to make sure
that anotations refer to existing types.
For builtin list[T]
the following operations are typechecked:
list[idx]
list[idx] = value
list += [...]
list.append(value)
list.insert(idx, value)
list.extend(iterator)
for i in list:
(Iterator)For builtin set[T]
these operations are typechecked:
set.add(value)
set.pop()
set.remove(value)
Value must be of T
set.update(other, ...)
value in set
Value must be of T
for i in set:
(Iterator)For builtin dict[K,V]
the supported typechecked operations are:
dict.get(key)
dict.items()
dict.keys()
dict.pop()
dict.popitem()
dict.setdefault(key, default)
default
is required, to avoid inserting None
as value into otherwise typed dicts.dict.update(other)
dict.update(key=value, ...)
dict.values()
key in dict
Key must be of K
del dict[key]
for k in dict
(Iterator)reversed(dict)
dict[key]
dict[key] = value
When executing a python file with the RUN button, the current working directory is set to
the directory of the file being executed. The __name__
attribute is set to the value
'__wypp__'
.
Please report them in the issue tracker.
You can debug the extension from Visual Studio Code:
npm install
npm run build
extension.ts
.