Skip to content

Commit

Permalink
Update scripts and config
Browse files Browse the repository at this point in the history
  • Loading branch information
you-n-g committed Jul 24, 2024
1 parent 3334ba9 commit dd1f2d9
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
35 changes: 35 additions & 0 deletions language/python/env_interact.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import subprocess
import os

# Define the shell command as a multi-line string
shell_command = """
cat << EOF > .env
TEST_VAR=123
EOF
"""

# Execute the shell command


import fire
class Exp:
def run(self):
subprocess.run(shell_command, shell=True, check=True)

def test_env(self):
# NOTE:
# If you only source the variables
# - It will fail;
#
# You have to use following methods to make it works
# 1) source the env
# if [ -f .env ]; then
# # Export each variable in the .env file
# export $(grep -v '^#' .env | xargs)
# fi
# 2) use dotenv
# dotenv run -- python app.py
print(os.environ["TEST_VAR"])

if __name__ == "__main__":
fire.Fire(Exp)
5 changes: 3 additions & 2 deletions language/python/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ def foo(self, x):


class MyMeta(type):
meta_attr = "meta attribute will become the attribute of class"
# But it will not present in __init__, __new__, __call__
# FIXME: it does not work in my Python
# meta_attr = "meta attribute will become the attribute of class"
# # But it will not present in __init__, __new__, __call__

def __new__(cls, clsname, bases, attrs):
# MetaClass的new代表创建子类, Class的new代表创建实例
Expand Down
30 changes: 28 additions & 2 deletions language/python/yield.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_yield_from():
res = yield from test_yield()
# return value of `yield from` & `yield` are very different.
# The yield will return the value
print(res)
print("test_yield_from get the return value:", res)
return res # This value can't be returned


Expand All @@ -21,6 +21,11 @@ def test_send():
print("yield ret:", x)


def test_yield_and_ret():
yield "yield value"
return "return value"


if __name__ == "__main__":

print("=================== yield from ===================")
Expand Down Expand Up @@ -48,4 +53,25 @@ def test_send():

# 总结
# - next 和 send 除了输入之外本质一样,都是让generate跑到下一个yield.
# - send是先把值输入到上一次yield的返回值,所以第一次调用send必须输入None
# - send是先把值输入到上一次yield的位置并且作为返回值,所以第一次调用send必须输入None(因为没有上一次yield)


print("------------------- test yield and return -------------------")
# Compare yield and return
gen = test_yield_and_ret()
# TODO: how to get "yield value" and "return value"?
print("test", next(gen))
try:
# Get the "yield value"
yield_value = next(gen)
print("Yielded value:", yield_value)

# Continue to the end of the generator to get the return value
next(gen)
except StopIteration as e:
# The return value is stored in the exception
return_value = e.value
print("Returned value:", return_value)

# TODO::
# How can I set the state of the generator (e.g. continue to run from a specific line, locals() ...)

0 comments on commit dd1f2d9

Please sign in to comment.