Do you know whether it's possible to setup WebStorm in the way it will import functions from functional programming (FP) module (lodash/fp/xxx)?
After reading "Why using _.chain is a mistake." I wrote few functions using flow function such as that in the example:
import map from "lodash/fp/map";
import flatten from "lodash/fp/flatten";
import sortBy from "lodash/fp/sortBy";
import flow from "lodash/fp/flow";
flow(
map(x => [x, x*2]),
flatten,
sortBy(x => x)
)([1, 2, 3]);
Unfortunately, I don't see option to import those functions from lodash/fp package in the context menu in WebStorm. Is there an option to enable them somehow?
Related
I have utils.py and there are two functions: runrealcmd, mol_with_atom_index
If I try to import the two function with the following code:
from utils import mol_with_atom_index, runrealcmd
It fails to import runrealcmd. The error message is like below:
ImportError: cannot import name 'runrealcmd' from 'utils'
However, if I try to import only the mol_with_atom_index with the following code:
from utils import mol_with_atom_index
It successes. The function of mol_with_atom_index can be imported in Jupyter notebook.
However, the function of runrealcmd cannot be imported in Jupyter notebook although both of the two functions are in the same utils.py file.
ImportError: cannot import name 'runrealcmd' from 'utils'
utils.py
from subprocess import Popen, PIPE, STDOUT
from IPython.core.magic import register_line_magic
#register_line_magic
def runrealcmd(command):
# Display instantly: https://stackoverflow.com/questions/52545512/realtime-output-from-a-shell-command-in-jupyter-notebook
process = Popen(command, stdout=PIPE, shell=True, stderr=STDOUT, bufsize=1, close_fds=True)
for line in iter(process.stdout.readline, b''):
print(line.rstrip().decode('utf-8'))
process.stdout.close()
process.wait()
def mol_with_atom_index(mol):
for atom in mol.GetAtoms():
atom.SetAtomMapNum(atom.GetIdx())
return mol
(Jupyter notebook) If you want to import the same-named function (previously imported with the same name) from the other path, you should restart the kernel first.
I'm trying to use a recorded case in OpenMDAO contained in "my_file.db"
when i execute the following code:
import openmdao.api as om
cr = om.CaseReader('my_file.db')
I get the following error:
ModuleNotFoundError: No module named 'groups'
'groups' is a folder from the openMDAO code that I used to record the case and now I'm trying to import it from a different directory. How can I redefine the path for om.CaseReader to look for the modules it needs?
try setting your PYTHONPATH, as discussed here:
https://bic-berkeley.github.io/psych-214-fall-2016/using_pythonpath.html
Solved using:
import os
dirname = os.path.dirname(__file__)
import sys
sys.path.append( dirname )
Does anybody now if there is any module that could let me import a graph (network) in Julia?
Working with Python, I used the graph-tool package, which served me very well! I have my graphs in .gt file format. Can I use any module in Julia, so that I can import them there?
I have searched for LightGraphs and Junet, which is fairly new but cannot seem to see any "import" section in the documentation.
The most straightforward solution is to convert your gt files to graphml format, which is compatible with LightGraphs, and is the recommended alternative format by graph-tool.
Suppose you have a ".gt" file that was generated in the past by the following python code:
from graph_tool.all import *
g = Graph()
v1 = g.add_vertex()
v2 = g.add_vertex()
e = g.add_edge(v1,v2)
g.save('g.gt')
Start a new python session, and convert from "gt" to "graphml" format:
import graph_tool as gt
g = gt.Graph()
g.load('g.gt')
g.save('g.xml.gz')
Then, in julia, use LightGraphs with the GraphIO package to load from the GraphML file:
using LightGraphs, GraphIO
D = loadgraphs("g.xml.gz", GraphMLFormat())
#> Dict{String,LightGraphs.AbstractGraph} with 1 entry:
# "G" => {2, 1} directed simple Int64 graph
If you'd like to use PyCall to perform the conversion directly from within julia (i.e. in a script), here's how:
using PyCall
#pyimport graph_tool as gt
G = gt.Graph()
G[:load]("g.gt")
G[:save]("g.xml.gz")
(Note that this implies python and the graph-tool library are already installed on your machine and accessible from julia).
In theory, if you prefer graph-tools and you're used to its syntax and wish to keep working directly with the .gt file format, you can use it via PyCall from within julia throughout as above. Whether this is preferable to migrating over to LightGraphs which is designed for julia though is another thing. It's your call :)
PS. Greetings from Leamington, fellow Leamingtonian!
Graph importing for LightGraphs is now in GraphIO.jl. Supported import formats currently include
GML
Graph6
GEXF
GraphML
Pajek NET
DOT
with more formats coming soon.
Lets say I have a Julia module, MyModule, that defines a function called conv() for 1D convolution. I would like to be able to call this function via MyModule.conv() in a file that imports or uses the file. Example:
import MyModule.conv
MyModule.conv()
However, I cannot get this syntax to work; Julia still calls Base.conv() instead of MyModule.conv(). I have tried all the different styles of using and import but cannot get this syntax to work.
Is this functionality available in Julia? I feel like I have seen this be implemented in other Julia packages but cannot find an example that works.
EDIT
Current setup is as follows; there is no reference to conv() anywhere in ModA outside of the definition.
module ModA
function conv(a, b)
println("ModA.conv")
end
end
Then, in another file,
import ModA
conv(x, y) #still calls Base.conv()
SOLVED
This was entirely my fault. The import wasn't working because of an incorrect LOAD_PATH calling a different version of the file than I thought was being called (finding the first one in the LOAD_PATH, which was not the one that I was editing). Entirely my fault...
Do you mean something like this?
module ModA
conv() = println("Calling ModA.conv")
end
module ModB # Import just conv from ModA
using ..ModA.conv # Could have used import ..ModA.conv instead
conv() # if we want to be able to extend ModA.conv
end
module ModC # Import ModA and use qualified access to access ModA.conv
import ..ModA
ModA.conv()
end
You must make sure not to make any reference to conv inside ModA before you have defined your own function, or it will already have looked up Base.conv and associated the name conv with that.
Question: What is the difference between using and import in Julia when I'm building my own module?
My guess based on reading the docs: using is used to bring another module into the name-space of the current module. import is used to bring specific types/functions/variables from other modules into the name-space of the current module.
So, how wrong am I?
The Julia Modules documentation states:
The import keyword [...] only
operates on a single name at a time. It does not add modules to be
searched the way using does. import also differs from using in that
functions must be imported using import to be extended with new
methods. [...] Functions whose names are only visible via using cannot be extended.
(Emphasis mine.)
For example, you can use import to add methods to Base.show to pretty-print your own types, but not with using.
There is also importall that imports all exported names of a module.
(This answer refers to Julia 0.6; the documentation was reworded for 1.0.)
The documentation (updated link for Julia 1.4) about this is excellent. Here's the excerpt which I find to be the most succinct summary:
(a demo module to make the examples below specific)
module MyModule
export x, y
x() = "x"
y() = "y"
p() = "p"
end
(this is a table in the documentation, but StackOverflow still won't add support for tables so... reformatted)
Command
using MyModule
in-scope: All exported names (x and y), MyModule.x, MyModule.y, and MyModule.p
extensible: MyModule.x, MyModule.y, and MyModule.p
using MyModule: x, p
in-scope: x and p
extensible: (nothing)
import MyModule
in-scope: MyModule.x, MyModule.y, and MyModule.p
extensible: MyModule.x, MyModule.y, and MyModule.p
import MyModule.x, MyModule.p
in-scope: x and p
extensible: x and p
import MyModule: x, p
in-scope: x and p
extensible: x and p
A summary of the main difference, in a way that I find easy to remember:
using NiceStuff allows usage access to exported names without the module qualifier, which import NiceStuff doesn't; and
import NiceStuff: nice allows extension access (adding methods) to the specified function without the module qualifier, which using NiceStuff: nice doesn't.
And a minor difference:
X as Y syntax is allowed for individual identifiers with both using and import (using Random: randstring as rstr, import Random: randstring as rstr) but for the module name itself, import Random as Rnd is allowed while using Random as Rnd is an error.
Some other points I found useful from the Modules docs page
using ModuleName is the only form for which export lists matter at all.
import NiceStuff is equivalent to using NiceStuff: NiceStuff.