Call a function in repl without brackets

2024/9/20 11:34:10

Would like to know if there is a way to call a function in python in repl just with the function name.

$ python -i interace.py
>>> load 834.png
>>> sharpen
>>> save

rather than

$ python -i interface.py
>>> load('834.png')
>>> sharpen()
>>> save()

reason being, I have a file instructions.txt that has the format as above. I wish to just do $ python -i interface.py < instructions.txt. And for the times when I don't have an instruction file, I can just enter my instructions manually.

Answer

There's no way to do this with the regular Python interpreter. Function calls must include parentheses.

Now, your instructions look a lot like shell syntax, so maybe what you could do is write your own simple parser. Try looking at shlex to start.


By the way, if it helps, IPython includes an %autocall setting that almost does what you want.

In [4]: print 'hello'                                                                                 File "<ipython-input-4-5a1ef41e7057>", line 1print 'hello'^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print('hello')?In [5]: %autocall                                                                                      
Automatic calling is: SmartIn [6]: print 'hello'                                                                                 
------> print('hello')
helloIn [7]: print 834.png                                                                                 
------> print(834.png)File "<ipython-input-7-3d64d8523fdd>", line 1print(834.png)^
SyntaxError: invalid syntax
https://en.xdnf.cn/q/119331.html

Related Q&A

how to work on a exist session in selenium with python?

I want to fill some field of a webpage and then send a request to it but this website has a very powerful login page to avoid sending requests for login from a robot so I cant log in with selenium bu…

how to find similarity between many strings and plot it

I have a xls file with one column and 10000 strings I want to do few things 1- make a heatmap or a cluster figure shows the similarity percentage between each string with another one.In order to find …

Python and Variable Scope

So I am recently new to Python, but I seem to be able to program some stuff and get it working. However Ive been trying to expand my knowledge of how things work in the language, and putting this simp…

How to check if element is orthogonally adjacent (next to) to existing elements?

Im trying to make a simple game where a building placed in a nested list must be next to another building. The problem I face is that if the building was placed at the sides, I cant use for loops to ch…

How to add new column(header) to a csv file from command line arguments

The output of the following code:-import datetime import csv file_name=sample.txt with open(file_name,rb) as f: reader = csv.reader(f,delimiter=",") …

Pattern matching and replacing in Python

Im trying to take a string that can be anything like "Hello here is a [URL]www.url.com[/URL] and its great." and be able to extract whatever is between [URL] and [/URL] and then modify the st…

In Python word search, searching diagonally, printing result of where word starts and ends

I have a friend of mine tutoring me in learning Python and he gave me this project where a user will read a word search into the program and the file includes a list of words that will be in the word s…

Selenium python : element not interactable

I am trying to scrape information from this website example website I need to get the version 2021 and search by code. Here is my code: from selenium import webdriver from selenium.webdriver.chrome.opt…

Date into matplotlib graph

How can I use a date from a Sqlite database on the x-axis to make a bar graph with matplotlib?If I convert the date to unix timestamp the graph works, but I would like to get something like this: http…

Non blocking IO - Programming model

In non blocking IO programming model, a thread blocked on data available channels, as shown below, in python,while True:readers, _, _ = select.select([sys.stdin, sock], [], []) # blocked select()for re…