Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve evaluation of class, decorated class, and decorated function definitions in Python with examples #618

Merged
merged 6 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 59 additions & 4 deletions dev/python/sandbox.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Should be able to evaluate all of these.

def add(a, b):
return a + b

Expand Down Expand Up @@ -49,6 +51,63 @@ def fn_with_multiline_str():

fn_with_multiline_str()

import csv
from datetime import datetime


# Class definition
# - from https://docs.python.org/3/tutorial/classes.html
class Dog:

def __init__(self, name):
self.name = name
self.tricks = []

def add_trick(self, trick):
self.tricks.append(trick)

d = Dog('Fido')
e = Dog('Buddy')
d.add_trick('roll_over')
e.add_trick('play dead')
d.tricks
e.tricks


# Class definition with decorator
# - from https://docs.python.org/3.10/tutorial/classes.html
from dataclasses import dataclass

@dataclass
class Employee:
name: str
dept: str
salary: int

john = Employee('john', 'computer lab', 1000)
john.dept
john.salary


# Function definition with decorator
# - https://docs.python.org/3.8/library/functools.html?highlight=decorator#functools.cached_property
from functools import lru_cache

@lru_cache(maxsize=None)
def fib(n):
if n < 2:
return n
return fib(n-1) + fib(n-2)

[fib(n) for n in range(16)]
# [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]

fib.cache_info()
# CacheInfo(hits=28, misses=16, maxsize=None, currsize=16)


# Asyncio samples
# - Add '-m asyncio' to the python command to evaluate these.

"""
async def slow_fn():
Expand All @@ -68,7 +127,3 @@ async def capture():
await capture()
result
"""


import csv
from datetime import datetime
Empty file modified docs/school
100755 → 100644
Empty file.
22 changes: 15 additions & 7 deletions fnl/conjure/client/python/stdio.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,21 @@
; "current form" and not be surprised that it wasn't what you thought.
(fn form-node?
[node]
(or (= "expression_statement" (node:type))
(= "import_statement" (node:type))
(= "import_from_statement" (node:type))
(= "with_statement" (node:type))
(= "function_definition" (node:type))
(= "for_statement" (node:type))
(= "call" (node:type))))
(log.dbg "form-node?: node:type =" (node:type))
(log.dbg "form-node?: node:parent =" (node:parent))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh good idea for a debug line, this will help diagnose some things.

(let [parent (node:parent)]
(if (= "expression_statement" (node:type)) true
(= "import_statement" (node:type)) true
(= "import_from_statement" (node:type)) true
(= "with_statement" (node:type)) true
(= "decorated_definition" (node:type)) true
(= "for_statement" (node:type)) true
(= "call" (node:type)) true
(and (= "class_definition" (node:type))
(not (= "decorated_definition" (parent:type)))) true
(and (= "function_definition" (node:type))
(not (= "decorated_definition" (parent:type)))) true
false)))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I assume this is inspired by the Julia client changes? Discovering that :InspectTree thing has made this a lot easier to reason about thankfully.

Not sure if we explicitly need to return true or false here though, I feel like we can get away with truthy and falsy. But if this is easier to reason about or you had issues without explicit boolean return values then this is fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I took inspiration from your changes to the Julia client while working on trying to upgrade an ancient Django project from version 1.9.

I was having some trouble getting the evaluation of classes and decorated classes/functions. In anger, I just did the simplest thing to keep me from confusing myself.


(fn with-repl-or-warn [f opts]
(let [repl (state :repl)]
Expand Down
81 changes: 52 additions & 29 deletions lua/conjure/client/python/stdio.lua

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.