I am wondering how can I convert Stata code into Python code.
For example, my Stata code looks like
if ("`var1'"=="") {
local l_QS "select distinct CountryName from `l_tableName'"
ODBCLoad, exec("`l_QS'") dsn("`dsn'") clear
}
And I want to convert it to Python code such as
if (f"{var1}"=="") :
l_QS = f"select distinct CountryName from {l_tableName}"
SQL_read(f"{l_QS}", dsn = f"{dsn}")
I am new to coding so I don't know what branch of computer science knowledge or what tools/techniques are relevant. I suppose knowledge about compilers and/or using regular expressions may help so I put those tags on my question. Any high-level pointers are appreciated, and specific code examples would be even better. Thanks in advance.
A very simple workaround would be to use the subprocess module included with python and write a basic command line wrapper to your scripts to use their functionality, then build your code from now on in python.
You could also look into possible API functionality in Stata if you have a whole lot of Stata code and it would take forever to convert it manually to python. This would require you to have access to a server and could be potentially costly, but would be cleaner than the subprocess module and wouldn't require the source code to be contained on your local machine. Also note that it's possible that Stata does not have tools to build an API.
As far as I am aware there are no projects that will directly parse a file from any language and convert it into python. This would be a huge project, although maybe with machine learning or AI it would be possible, though still very difficult. There are libraries for wrapping code in C and C++ (others too I'm sure I just know that these are available), but I can't find anything for Stata.
Related
I am new to Pycharm; however, I want to take advantage of my R and Python knowledge. I am a big fan of both languages, and I am constantly learning more about them.
I am hoping to pass an R variable to a Python variable, similar to Jupyter Notebook.
I could not find any example code anywhere of doing this.
R code
x <- 5
python code
# Some conversion method needs to be added
print(x)
Python Console
>>>5
This is possible because Jupyter provides its own Kernel that code runs in. This kernel is able to translate variables between languages.
Pycharm does not provide a kernel, and instead executes Python code directly through an interpreter on your system. It's similar to doing python my_script.py. AFAIK vanilla Pycharm does not execute R at all.
There are plugins for Pycharm that support R and Jupyter notebooks. You might be able to find one that does what you want.
I usually solve this problem by simply adhering to the Unix philosophy:
Rscript foo.R | python bar.py
where foo.R prints the data to STDOUT and bar.py expects to read it from STDIN (you could also read/write from files). This approach of course requires you to define a clear interface of data between the scripts, rather than passing all the variables indiscriminately, which I don't personally mind because I do that anyway as a matter of design best practices.
There are also packages like reticulate which will allow you to call one language from the other. This might be more useful if you like to switch languages a lot.
Thanks for the above answer! That is great. I did find solution that could help other people that use pycharm.
If you have installed the R plug in option, you can use the following code.
Python code to save to feather file:
```
import pyarrow.feather as feather
data = pd.read_csv('C:/Users/data.csv')
feather.write_feather(data, 'C:/Users/data.feather')
```
R code to retrieve feather file
```
library(tidyverse)
library(feather)
library(arrow)
data <- arrow::read_arrow('C:/Users/data.feather')
print(data)
```
However, this process seems very similar to writing a file to csv in Python, then uploading the csv into R. There could be some type of lightweight storage difference, speed processing, and language variable agnostic/class translation. Below is the official documentation: Git: https://github.com/wesm/feather
Apache Arrow: https://arrow.apache.org/docs/python/install.html
RStudio: https://www.rstudio.com/blog/feather/
I currently have a quite big program written in FoxPro (prg file). Now FoxPro is actually really getting outdated, so what I want to ask is the following:
Is it possible to convert all the FoxPro code (.prg file) to Python? Is there a simple trick/code for this? Python has to become my main coding platform, so I don't need to use FoxPro anymore for when I want to adjust some code or something else. Does someone have experience with this?
I hope to hear from you, appreciate your help.
PS: Please note that I am quite new to Python.
There are plenty of people from the FoxPro community who migrated into the Python world certainly, but there are no tools that I know of for converting source code. As with most of these 'can I convert from language x to language y' questions, you would spend more time getting the result into shape than you would just documenting and rewriting.
There are some Python scripts that implement common Visual FoxPro functions in the download section on Leafe.com which might help.
You might want to check out this question.
There is also vfp2py.
Python comes with support for excellent SQLite database built-in with the sqlite3 module. (You are probably aready using sqlite, but you might not even know it.) Storing the data in an sqlite database means you will not have to worry about not being able to access it for the rest of your life.
I've got a kind of weird question--but would be immensely useful if it is possible--in Maya using Python, can I take in several points of user input and have Python create a separate script for me? In this instance, I want to take in controller and locator names and have Python spit out a complete IKFK match script also in Python (it's really just a lot of getAttr and setAttr commands, although with 6 if statements per limb for PV matching.) The only other wrinkle there is that it has to be able to prefix hierarchy names in the script if necessary if the rig is imported into a new scene rather than just opened. There's an expression component to my switches that it would be nice if Python could make for me, too.
Is this possible or am I crazy?
That's no problem. Just write a textfile with a .py extension into a path where maya can find it. Then you have to import it somewhere. Creating expressions is not a problem either.
Maybe it could make sense to think about the approach you choose. Imagine you have written a dozen of these new python files and you discover a problem in the script, you will have to redo it. I'd try to collect all data and only write the required informations into a textfile e.g. in json format. Then you can read the data and rebuild your skeletons.
How can I run c/c++ code within python in the form:
def run_c_code(code):
#Do something to run the code
code = """
Arbitrary code
"""
run_c_code(code)
It would be great if someone could provide an easy solution which does not involve installing packages. I know that C is not a scripting language but it would be great if it could do a 'mini'-compile that is able to run the code into the console. The code should run as it would compiled normally but this needs to be able to work on the fly as the rest of the code runs it and if possible, run as fast as normal and be able to create and edit variables so that python can use it. If necessary, the code can be pre-compiled into the code = """something""".
Sorry for all the requirements but if you can make the c code run in python then that would be great. Thanks in advance for all the answers..
As somebody else already pointed out, to run C/C++ code from "within" Python, you'd have to write said C/C++ code into an own file, compile it correctly, and then execute that program from your Python code.
You can't just type one command, compile it, and execute it. You always have to have the whole "framework" set up. You can't compile a program when you haven't yet written the } that ends the class/function/statement 20 lines later on. At this point you'd already have to write the whole C/C++ program for it to work. It's simply not meant to be interpreted on the run, line by line. You can do that with python, bash/dash/batch, and a few others. But C/C++ definitely isn't one of them.
With those come several issues. Firstly, the C/C++ part probably needs data from the Python part. I don't know of any way of doing it in RAM alone (maybe there is one, but I don't know), so the Python part would have to write it into a file, the C/C++ part would read and process it, then put the processed data into another file, and then the Python part would have to read that and continue.
Which brings another point up. Here we're already getting into multi-threading territory, because the moment you execute that C/C++ program you're dealing with a second thread. So, somehow, you'd have to coordinate those programs so that the Python part only continues once the C/C++ part is done. Shouldn't be a huge problem to get running, but it can be a nightmare to performance and RAM if done wrongly.
Without knowing to what extent you use that program, I also like to add that C/C++ isn't platform-independent like Python. You'll have to compile that program for every single different OS that you run it on. That may come with minor changes to the code and in general just a lot of work because you have to debug and test it for every single system.
To sum up, I think it may be better to find another solution. I don't know why you'd want to run this specific part in C/C++, but I'd recommend trying to get it done in one language. If there's absolutely no way you can get it done in Python (which I doubt, there's libraries for almost everything), you should get your Python to C/C++ instead.
If you want to run C/C++ code - you'll need either a C/C++ compiler, or a C/C++ interpreter.
The former is quite easy to arrange (though probably not suitable for an end user product) and you can just compile the code and run as required.
The latter requires that you attempt to process the code yourself and generate python code that you can then import. I'm not sure this one is worth the effort at all given that even websites that offer compilation tools wrap gcc/g++ rather than implement it in javascript.
I suspect that this is an XY problem; you may wish to take a couple of steps back and try to explain why you want to run c++ code from within a python script.
I have list of files and General Names of files stored in a textfile.
I like to use this to make some shortcuts in windows.
Although in my company I cannot access the Py Win Module. I was wondering if there is an Easy workaround to do this in Python. Or if I have to look at other languages
There is a lot of Doucmention for the VBA side , but I m very much a novice using this language
https://msdn.microsoft.com/en-us/library/fywyxt64
Can this be done with Python Without using Py Win ?