Python script that uses pycharm's refactoring methods - python

I am trying to create a script that will do the following.
Iterate over each file in project.
_Iterate over each method in the file.
__if some_condition(method_name):
___user PyCharm's refactoring system to refactor the given method.
Couldn't find any reference to the above.
Does not request a given script(though wouldn't mind), any reference and start guidance would be welcome.
Using
python 2.7
PyCharm Community Edition 4.5.3

PyCharm is written in Java. There is no way to invoke PyCharm's refactoring system from a Python script.
Instead, you can write a plugin for PyCharm in Java, and implement that logic through PyCharm's plugin API.

Related

Autocomplete for Automation objects in VS Code and Python

I have the Python Extensions for Windows installed. Within the PythonWin IDE I can get autocomplete on Automation objects (specifically, objects created with win32com.client.Dispatch):
How can I get the same autocomplete in VS Code?
I am using the Microsoft Python extension.
The Python Windows Extensions has a tool called COM Makepy, which apparently generates Python representations of Automation objects, but I can't figure out how to use it.
Update
Apparently, the Microsoft Python extension uses Jedi for autocompletion.
I've filed an issue on the extension project on Github.
Note that in general I have Intellisense in Python; it's only the Intellisense on Automation objects that I am missing.
Review
I have confirmed your problem in VSCode, although it may be possible IntelliSense works fine. Note Ctrl+Space invokes suggestions for a pre-defined variable:
However, there does not appear to be public attributes available for win32com.client by default. This may be why IntelliSense does not appear to work.
Test
Having installed win32com for Python 3.6, I have confirmed the following code in Jupyter notebook, IPython console and the native Python REPL:
import win32com.client
app = win32com.client.Dispatch("Word.Application")
len(dir(app))
# 55
[x for x in dir(app) if not x.startswith("_")]
# []
This issue of hidden attributes is not a new. Please confirm this test in another environment/IDE. It may be your environment or particular version of PythonWin pre-loads certain variables in the global namespace.
Verify the following:
The Python extension is installed
A Python interpreter is selected
A Python file is selected; this starts up the Python server
References
Post by the extension's creator for troubleshooting autocompletion issues.
Thread on how autocompletion works via PyScriptor
I don't think that the example you show with PythonWin is easily reproducible in VS Code. The quick start guide of win32com itself (cited below) says, its only possible with a COM browser or the documentation of the product (Word in this case). The latter one is unlikely, so PythonWin is probably using a COM browser to find the properties. And because PythonWin and win32com come in the same package, its not that unlikely that PythonWin has a COM browser built in.
How do I know which methods and properties are available?
Good question. This is hard! You need to use the documentation with the
products, or possibly a COM browser. Note however that COM browsers
typically rely on these objects registering themselves in certain
ways, and many objects to not do this. You are just expected to know.
If you wanted the same functionality from the VS Code plugin a COM browser would have to be implemented into Jedi (IntelliSense of the VS Code plugin).
Edit: I found this suggestion, on how a auto-complete, that can find these hidden attributes, could be done:
These wrappers do various things that make static analysis difficult,
unless we were to special case them. The general solution for such
cases is to run to a breakpoint and work in the live runtime state.
The auto-completer should then include the complete list of symbols
because it inspects the runtime.
The conversation is from an mailing list of the python IDE wingwide. Here you can see, that they implemented the above mentioned approach:
I think your problem is related to defining Python interpreter.
Choose proper Python interpreter by executing python interpreter command in VS Code command palette by pressing f1 or ctrl+shift+p key.

Which IronPython editor I can use to develop scripts for Tibco Spotfire controls

Can we use any IRONPython editor to develop scripts for Tibco Spotfire controls.
Can we use IDLE editor to develop IRONPython scripts for Tibco Spotfire? If yes then how to integrate the tibco module with IDLE editor, Can anyone help on this??
You should be able to use any development tool (ide) which supports ironpython. One of the best in my point of view is PTVS (Python Tools for Visual Studio), just search for it. But when you want some thing very lightweight with only some syntax hilighting, i prefer using Visual Studio Code or Atom. But PTVS has a lot of nice features. One of the most important ones are those for debugging, because they prevent you from using some console printing or some thing similar as debugging tool. Just take a look at it.
EDIT
As far as i can see, it should work just fines with PTVS. Taking a look at this, is't just some API as any other api: API-Doc
Spotfre has its own IDE for developing scripts but it is very poor one when analysing its functionalities. I dont think you can use any IDE to debug the scripts but you can at least use the one suggested by BendEg to make creation of the code more 'pleasant'.
Spotfire uses IronPython, which is a .NET implementation of python. In other words, is .NET driven by python. To test simple python functions, you can use CodeSkulptor, a cloud based python interpreter. For IronPython, you can use this java based online version but again, this is to test simple scripts

Is it possible to implement automatic error highlighting for Python?

Are there any IDEs for Python that support automatic error highlighting (like the Eclipse IDE for Java?) I think it would be a useful feature for a Python IDE, since it would make it easier to find syntax errors. Even if such an editor did not exist, it still might be possible to implement this by automatically running the Python script every few seconds, and then parsing the console output for error messages.
eclipse+pydev
pycharm
many others ....
If you use VIM or don't have a problem with it, try this extension. https://github.com/klen/python-mode
This is for Emacs as well: https://github.com/gabrielelanaro/emacs-for-python
Also pycharm and eclipse with pydev work fine.
If I don't use vim I really enjoy spyder. It is easy to use and has some really nice features, like integrated debugging and profiling, graphical variable explorer and object inspector. The latter shows, e.g., the integrated documentation for every function of class you use.
I built an extension to Eclipse and PyDev that does what you describe, it runs the Python code as you're typing, and displays all the variable values and any exceptions that occur. It's called Live Coding in Python, and the web site has a tutorial and a demo video.
PyDev can highlight some problems in your code by analysing it, and Live Coding in Python can show you problems that happen when you run it.

Jythonc missing

I just installed Jython 2.5.1. I want to convert my Python file into Java class file and it is instructed on the website to use the jythonc command-line tool but I can't find it. Does anyone know where I could find it?
Basically what i was trying to accomplish is to get my Python code running client-side in a browser and the best way seemed to be by creating an applet using Jython. I don't want to create a desktop application and using Silverlight/IronPython is out of the question. Any other ideas are welcomed.
Cheers!
You can still compile your python-code to class-files:
import compileall;
compileall.compile_dir('Lib'); # to compile yor Lib-Dir
should work with 2.5 jython
i use it to create class-files to put in jars :-)
Jythonc was removed in Jython 2.2 and is no longer supported. The official way to embed Jython code in Java is to create an instance of the interpreter to run the Jython code directly. There is an article on this here.
Personally I preferred the jythonc method and hope it will be reinstated in a future version of Jython, even though it had a number of issues.

Call from Objective-C into Python

bbum posted an outline of how to do this, but I'm unable to complete the details. Where does the Python code go, and how will my Objective-C code know about it? How would I do it compiling on the command line?
Source here:
Calling Python From Objective-C
I have posted a full explanation of how to do this to my weblog as it is quite a bit longer than something I would post here.
The abstract summary remains the same: use an abstract class to provide the type information necessary to make the C compiler happy and the metadata necessary to make the bridge happy.
Unfortunately the story for using Python via PyObjC from within an Objective-C app is not very good at the moment. py2app which ships with PyObjC can compile loadable bundles (i.e. can be loaded via NSBundle), which seems like the best approach: define an NSObject subclass in python that implements a protocol (obtained via objc.protocolNamed) that you define in Objective-C, then compile this python file into a loadable bundle via py2app (which uses a standard setup.py). Unfortunately, py2app hasn't had much love, especially the plugin (loadable bundle) target, and a serious memory leak was introduced sometime around 10.5 such that any data passed from python to Objective-C from a py2app-compiled bundle leaks. Yuck.
PyObjC manipulates the Objective-C runtime in accordance with the ObjC-related code executed in Python, thus to be able to call python code from Objective-C, the general outline goes like
Write PyObjC wrapper around python code
Execute code declaring PyObjC wrapper to add these definitions to the ObjC runtime
Call PyObjC wrapper from Objective-C. Because it's declared at runtime, the symbols aren't available at compile time, so you'll have to use NSClassFromString et al. to instantiate the class. It's helpful to declare a #protocol with the appropriate methods so that the Objective-C compiler doesn't complain about missing methods.
If you have flexibility, the best option is to use the Cocoa-Python app templates (i.e. create a Python app), and then load your Objective-C code as a loadable bundle from within Python. This takes care of managing the Python interpreter for you.
Otherwise, with the code in main.m of the Cocoa-Python app template, you should be able to create a Python interpreter, execute your PyObjC code and then continue on. Obviously, the interpreter needs to be kept running so that your python code can execute, so you'll likely have to do this from a separate thread. As you can see this can get a little hairy. Better to go with the Python app, as described above.
Keep in mind that PyObjC is not guaranteed to play well with the Objective-C garbage collector, so all of these options require that your Objective-C code not use GC.
Google is your friend. Performing a search on the string "Cocoa Python" quickly turned up PyObjc.

Categories

Resources