is it possible to stitch a python script using stitch() with knitr?
I want to produce a simple document by typing:
stitch('someScript.py')
analogous to:
stitch('someScript.R')
As I probably have answered you on Twitter, you can try
library(knitr)
opts_chunk$set(engine = 'python')
stitch('script.py')
But note the Python support in knitr is very limited at the moment. You may or may not be satisfied by it.
Related
I want to change variable (and method) names of a given python code with a set of given words. (If someone is familiar with ProGuard for Android, this will be very similar to that.) For example, I want to do something like below:
python renamer.py --input ./some-source.py --wordlist ./nice_words.txt
I took a look of rope (python library) and other obfuscation tools. But I could not find what I wanted. What will be a good way to do what I want to do?
I found the solution. An open source project called Carbon (https://github.com/0sir1ss/Carbon) can be used for this purpose by slightly modifying the code.
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 have some Python modules, some of them require more than 20 others.
My question is, if there a tool is which helps me to bundle some Python modules to one big file.
Here a simple example:
HelloWorld.py:
import MyPrinter
MyPrinter.displayMessage("hello")
MyPrinter.py:
def displayMessage(msg):
print msg
should be converted to one file, which contains:
def displayMessage(msg):
print msg
displayMessage("hello")
Ok, I know that this example is a bit bad, but i hope that someone understand what i mean and can help me. And one note: I talk about huge scripts with very much imports, if they were smaller I could do that by myself.
Thanks.
Aassuming you are using Python 2.6 or later, you could package the scripts into a zip file, add a __main__.py and run the zip file directly.
If you really want to collapse everything down to a single file, I expect you're going to have to write it yourself. The source code transformation engine in lib2to3 may help with the task.
You cannot and should not 'convert them into one file'.
If your application consists of several modules, you should just organize it into package.
There is pretty good tutorial on packages here: http://diveintopython3.org/packaging.html
And you should read docs on it here: http://docs.python.org/library/distutils.html
Pip supports bundling. This is an installation format, and will unpack into multiple files. Anything else would be a bad idea, as it would break imports and per-module metadata.
In Python 2.6, I used matplotlib to make some simple graphs. However, it is incompatible with Python 3.1.
What are some alternative modules that can accomplish the same thing without being very complex?
You say you want to create some simple graphs but haven't really said how simple or what sort of graphs you want. So long as they aren't too complex you might want to consider using the Google Chart API.
e.g.
That has some advantages: you just have to construct a URL that describes the desired chart so there shouldn't be any issues using it from Python 3.x. Of course there are disadvantages also: you need to have an internet connection when generating the chart, and you might not have the chart styles you have been using with matplotlib.
If you don't want to construct the URLs directly there is at least one Python wrapper for the charts API. It doesn't work directly on Python 3.x, but running it through 2to3 seems to convert it successfully.
A stable version supporting Python 3 has since been released: official announcement.
I wrote a small example that runs in python 3 and uses the google chart api (as suggested by Duncan, I wrote the solution after seeing this post).
It is not ideal since it adds a dependency one a 3rd party that might break backward compatibility at any time, but the graphs are nice and there is absolutely no added dependency on the python code. Worth considering for not 'mission critical code'.
You can find/download the example here. Here is the graph that it generates from data in a .xml file:
# build the query with template parameters
query ="http://chart.apis.google.com/chart?chxl=0:__X_LABELS__&chxp=__X_LABELS_POS__&chxr=0,__MIN_TIME__,__MAX_TIME__|1,__MIN_WEIGHT__,__MAX_WEIGHT__&chxs=0,676767,11.5,0,lt,676767|1,676767,11.5,0,lt,676767&chxt=x,y&chs=800x300&cht=lc&chco=3072F3&chds=__MIN_WEIGHT__,__MAX_WEIGHT__&chd=t:__COMMASEP_WEIGHT__&chdl=Weight&chdlp=b&chls=2,4,1&chma=5,5,5,25&chtt=Your+Weight+Timeline"
[...]
# relace template with data
query = query.replace('__X_LABELS__', strXLabels)
query = query.replace('__X_LABELS_POS__', strXLabelsPos)
query = query.replace('__MIN_TIME__', str(min(lst_dateEpoch)))
query = query.replace('__MAX_TIME__', str(max(lst_dateEpoch)))
[...]
# use 'urllib.request' to download the data & write to file
sock = urllib.request.urlopen(query)
image_bytes = sock.read()
sock.close()
fh = open('Weight_GoogleGraphApi.png', 'wb')
fh.write(image_bytes)
fh.close()
Maybe PyQwt? They claim 3.x compatibility. I've only used Qwt (the C++ lib PyQwt is based on) but I found it fairly useful.
rpy2 is providing access to the graphics capabilities of R, and rpy2 is becoming compatible with Python 3 (thanks to the help of Google for funding Greg over the summer).
Code against the current dev branch is in a patch queue.
edit: rpy2 2.2.0 is working with Python 3.2
MathGL (GPL plotting library) have Python interface which work with Python 3 too.
Matplotlib binaries for python 3.x (windows) are avaliable. http://www.lfd.uci.edu/~gohlke/pythonlibs/
As an alternative to installing subversion to grab the source, Numpy's SF files page has the latest copy of 1.5 in a few different (Windows-friendly) formats:
http://sourceforge.net/projects/numpy/files/NumPy/1.5.0b1/
There are at least two graphing libraries using PyQt, namely PyQwt and PyQtGraph. I've been using PyQwt with Python 2.6 for a few weeks now and it is quite handy. The documentation isn't great, and most of the time I need to refer to either the Qwt docs or the examples - although the times I've had to look at the docs have been few and far between, it is VERY easy to use. I tried building it against python 3.1 just now though without success. I coulnd't find the tar package for 5.2.1 which is the only version compatible with python 3.0 and there isn't anything on MacPorts for that either.
There is also a fairly complete looking list of plotting libraries on at python.org http://wiki.python.org/moin/NumericAndScientific/Plotting
The particular alias I'm looking to "class up" into a Python script happens to be one that makes use of the cUrl -o (output to file) option. I suppose I could as easily turn it into a BASH function, but someone advised me that I could avoid the quirks and pitfalls of the different versions and "flavors" of BASH by taking my ideas and making them Python scripts.
Coincident with this idea is another notion I had to make a feature of legacy Mac OS (officially known as "OS 9" or "Classic") pertaining to downloads platform-independent: writing the URL to some part of the file visible from one's file navigator {Konqueror, Dolphin, Nautilus, Finder or Explorer}. I know that only a scant few file types support this kind of thing using some other command-line tools (exiv2, wrjpgcom, etc). Which is perfectly fine with me as I only use this alias to download single-page image files such as JPEGs anyways.
I reckon I might as well take full advantage of the power of Python by having the script pass the string which is the source URL of the download (entered by the user and used first by cUrl) to something like exiv2 which could write it to the Comment block, EXIF User Comment block, and (taking as a first and worst example) Windows XP's File Description field. Starting small is sometimes a good way to start.
Hope someone has advice or suggestions.
BZT
The relevant section from the Bash manual states:
Aliases allow a string to be
substituted for a word when it is used
as the first word of a simple command.
So, there should be nothing preventing you from doing e.g.
$ alias geturl="python /some/cool/script.py"
Then you could use it like any other shell command:
$ geturl http://example.com/excitingstuff.jpg
And this would simply call your Python program.
I thought Pycurl might be the answer. Ahh Daniel Sternberg and his innocent presumptions that everybody knows what he does. I asked on the list whether or not pycurl had a "curl -o" analogue, and then asked 'If so: How would one go about coding it/them in a Python script?' His reply was the following:
"curl.setopt(pycurl.WRITEDATA, fp)
possibly combined with:
curl.setopt(pycurl.WRITEFUNCITON, callback) "
...along with Sourceforge links to two revisions of retriever.py. I can barely recall where easy_install put the one I've got; how am I supposed to compare them?
It's pretty apparent this gentleman never had a helpdesk or phone tech support job in the Western Hemisphere, where you have to assume the 'customer' just learned how to use their comb yesterday and be prepared to walk them through everything and anything. One-liners (or three-liners with abstruse links as chasers) don't do it for me.
BZT