Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 :

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"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading