'python with statement one liner returns invalid syntax
I have a snippet below.. Basically, my goal is to execute my whole python snippet with just one line... But it always returns an error when it comes to the with statement
$ python
>>> import os
>>> phone = os.environ['PHONE'];
>>> api_id = os.environ['API_ID'];
>>> api_hash = os.environ['API_HASH']
>>> from telethon.sync import TelegramClient
>>> from telethon import functions, types; with TelegramClient(phone, api_id, api_hash) as client: print('test')
With the code above it will return something like below:
>>> from telethon.sync import TelegramClient; with TelegramClient(phone, api_id, api_hash) as client: print('$2')
File "<stdin>", line 1
from telethon.sync import TelegramClient; with TelegramClient(phone, api_id, api_hash) as client: print('$2')
^^^^
SyntaxError: invalid syntax
UPDATE: If you guys want to know the whole snippet used with bash level. Feel free to check snippet below:
docker exec ${item} python manage.py shell -c "import os; \
from telethon.sync import TelegramClient; \
from telethon import functions, types; \
phone = os.environ['PHONE']; \
api_id = os.environ['API_ID']; \
api_hash = os.environ['API_HASH']; \
with TelegramClient(phone, api_id, api_hash) as client: \
result = client(functions.channels.JoinChannelRequest(channel='$2'));\
result2 = client(functions.channels.JoinChannelRequest(channel='$3'));\
";
Above is the whole code including the one used in bash script if you are interested to specifically
Solution 1:[1]
Only simple statements may be joined with a ; on a single line:
Several simple statements may occur on a single line separated by semicolons.
A with statement is a compound statement, and as such is not eligible to appear on a line after a ;.
Based on your update, you may just need to remove the line continuations and the semicolons. You already have multiple physical lines; there's no reason to try to create one logical line.
docker exec ${item} python manage.py shell -c "import os
from telethon.sync import TelegramClient
from telethon import functions, types
phone = os.environ['PHONE']
api_id = os.environ['API_ID']
api_hash = os.environ['API_HASH']
with TelegramClient(phone, api_id, api_hash) as client:
result = client(functions.channels.JoinChannelRequest(channel='$2'))
result2 = client(functions.channels.JoinChannelRequest(channel='$3'))
";
You probably should not try to embed the shell parameters in the script, but rather pass them as arguments. Not knowing what manage.py is, you might try
docker exec ${item} python manage.py shell -c "import os
import sys
from telethon.sync import TelegramClient
from telethon import functions, types
phone = os.environ['PHONE']
api_id = os.environ['API_ID']
api_hash = os.environ['API_HASH']
with TelegramClient(phone, api_id, api_hash) as client:
result = client(functions.channels.JoinChannelRequest(channel=sys.argv[1]))
result2 = client(functions.channels.JoinChannelRequest(channel=sys.argv[2]))
" "$2" "$3"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
