I am using Python 3.6 and latest version of vscode.
My folder structure:
/folder
/src
src.py
/tests
src_test.py
src.py:
class Addition:
def __init__(self, a, b):
self.a = a
self.b = b
def run(self):
return self.a + self.b
src_test.py:
import unittest
from ..src.addition import Addition
class TestAddition(unittest.TestCase):
def test_addition(self):
inst = Addition(5, 10)
res = inst.run()
self.assertEqual(res, 16)
if( __name__ == "main"):
unittest.main()
here is my project settings.json:
{
"python.testing.unittestArgs": [
"-v",
"-s",
".",
"-p",
"*_test.py"
],
"python.testing.pytestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.unittestEnabled": true
}
Then under project root folder:
python3 -m unittest tests/src_test.py
File "/usr/lib/python3.6/unittest/loader.py", line 153, in loadTestsFromName
module = __import__(module_name)
ModuleNotFoundError: No module named 'tests.src_test'
You are missing an __init__.py file under the tests folder, and the import your module needs a tweak.
The __init__.py allows Python to go up in the folder structure and reach the src.py module (another __init__.py under src would be nice, but it is not necessary to make the vscode tests work).
This is a working folder structure:
.
├── src
│ └── src.py
└── tests
├── __init__.py
└── src_test.py
Also, change the line in src.py:
(OLD) from ..src.addition import Addition
(NEW) from src.src import Addition
Related
I have a directory contains a bunch of txt files:
dir/train/[train1.txt, train2.txt, train3.txt]
I'm able to read a single file, if I define following in a config.yaml
file_name: ${paths.data_dir}/train/train1.txt
So I get the str and I used np.loadtxt(self.hparams.file_name)
I tried
file_name: ${paths.data_dir}/train/*
So I have List[str], I then loop over file_name
dat = []
for file in self.hparams.file_name:
dat.append(np.loadtxt(file))
but it didn't work out.
You could define an OmegaConf custom resolver for this:
# my_app.py
import pathlib
from pathlib import Path
from typing import List
from omegaconf import OmegaConf
yaml_data = """
paths:
data_dir: dir
file_names: ${pathlib_glob:${paths.data_dir}, 'train/*'}
"""
def pathlib_glob(data_dir: str, glob_pattern: str) -> List[str]:
"""Use Pathlib glob to get a list of filenames"""
data_dir_path = pathlib.Path(data_dir)
file_paths: List[Path] = [p for p in data_dir_path.glob(glob_pattern)]
filenames: List[str] = [str(p) for p in file_paths]
return filenames
OmegaConf.register_new_resolver("pathlib_glob", pathlib_glob)
cfg = OmegaConf.create(yaml_data)
assert cfg.file_names == ['dir/train/train3.txt', 'dir/train/train2.txt', 'dir/train/train1.txt']
Now, at the command line:
mkdir -p dir/train
touch dir/train/train1.txt
touch dir/train/train2.txt
touch dir/train/train3.txt
python my_app.py # the assertion passes
$ protoc --go_out=module=github.com/redmcme/go-protos:. \
--go-grpc_out=module=github.com/redmcme/go-protos:. \
--proto_path ../protos \
../protos/*/**
teams.team: File not found.
teams/season.proto:3:1: Import "teams.team" was not found or had errors.
teams/season.proto:22:18: "team.Team" is not defined.
Build command ^
$ ltt ../protos
../protos
├── skyblock
│ └── player.proto
└── teams
├── season.proto
└── team.proto
Directory tree ^
// team.proto
syntax = "proto3";
package teams.team;
option go_package = "github.com/redmcme/go-protos/teams/team";
...
Team proto ^
// season.proto
syntax = "proto3";
import "teams.team";
package teams.season;
option go_package = "github.com/redmcme/go-protos/teams/season";
Season proto ^
Does anyone know why protoc failed to find the import
import "teams.team" is wrong way
the true way is
import "team.proto" or import “teams/team.proto”
Say I have the following multi-project structure:
my-project/
├── build.sbt
├── app1/
├── app2/
├── app3/
├── sharedA/
└── sharedB/
I use sbt-assembly to create fat JARs for the app subprojects. The shared subprojects are just library code so I disable AssemblyPlugin for those.
I use the default root project syntax so the subprojects are aggregated without me having to list them all out.
name := "my-project"
lazy val app1 = (project in file("app1"))
.dependsOn(sharedA)
lazy val app2 = (project in file("app2"))
.dependsOn(sharedB)
lazy val app3 = (project in file("app3"))
.dependsOn(sharedA, sharedB)
lazy val sharedA = (project in file("sharedA"))
.disablePlugins(AssemblyPlugin)
lazy val sharedB = (project in file("sharedB"))
.disablePlugins(AssemblyPlugin)
When I run sbt assembly, this mostly works: I get assembly jars for the apps and none for the shareds. However, I also get a my-project-assembly.jar which I don't need. How do I disable this?
If I disable from the top
name := "my-project"
disablePlugins(AssemblyPlugin)
...
Then the plugin is disabled completely, even for subprojects.
I know I can explicitly declare a root project, and this works:
lazy val root = (project in file("."))
.aggregate(app1, app2, app3, sharedA, sharedB)
.disablePlugins(AssemblyPlugin)
...
But, in reality, there can be a lot of subprojects so I would like to stick to the default root project style.
If you discard the paths in the assemblyMergeStrategy i think it will run as you wish.
assemblyMergeStrategy in assembly := {
case PathList("sharedA", xs # _*) => MergeStrategy.discard
case PathList("sharedB", xs # _*) => MergeStrategy.discard
...
}
I have a very organized build file that is composed of the following scala files:
Build.scala - the main Build file
Dependencies.scala - where I define the dependencies and the versions
BuildSettings.scala - where I define the build settings
plugins.sbt
A snippet of the Build.scala is as below:
import sbt._
import Keys._
object MyBuild extends Build {
import Dependencies._
import BuildSettings._
import NativePackagerHelper._
// Configure prompt to show current project
override lazy val settings = super.settings :+ {
shellPrompt := { s => Project.extract(s).currentProject.id + " > " }
}
// Define our project, with basic project information and library dependencies
lazy val project = Project("my-project", file("."))
.settings(buildSettings: _*)
.settings(
libraryDependencies ++= Seq(
Libraries.scalaAsync
// Add your additional libraries here (comma-separated)...
)
).enablePlugins(JavaAppPackaging, DockerPlugin)
}
All the 4 files that I mentioned above are in the same directory which is inside the project directory. But when I run this build file, I get the following error:
not found value: NativePackagerHelper
Any clues why his this?
I figured out what the problem was. I had to use the following in my build.properties
sbt.version=0.13.11
I originally had 0.13.6 and it was causing the import statements to fail!
Recently I've changed from CMake to Premake (v5.0.0-alpha8) and I'm not quite sure how to achieve the the following in Premake.
I want to include some dependencies so in CMake I can do something like this:
target_link_libraries(${PROJECT_NAME}
${YALLA_ABS_PLATFORM}
${YALLA_LIBRARY})
The above will add the paths of these libraries (dir) to "Additional Include Directories" in the compiler and it will also add an entry (lib) to "Additional Dependencies" in the linker so I don't need to do anything special beyond calling target_link_libraries.
So I expected that when I'm doing something like this in Premake:
links {
YALLA_LIBRARY
}
I'd get the same result but I don't.
I also tried to use the libdirs but it doesn't really work, I mean I can't see the library directory and its subdirectories passed to the compiler as "Additional Include Directories" (/I) or Yalla.Library.lib passed to the the linker as "Additional Dependencies".
Here is the directory structure I use:
.
|-- src
| |-- launcher
| |-- library
| | `-- utils
| `-- platform
| |-- abstract
| `-- win32
`-- tests
`-- platform
`-- win32
The library dir is defined in Premake as follow:
project(YALLA_LIBRARY)
kind "SharedLib"
files {
"utils/string-converter.hpp",
"utils/string-converter.cpp",
"defines.hpp"
}
The platform dir is defined in Premake as follow:
project(YALLA_PLATFORM)
kind "SharedLib"
includedirs "abstract"
links {
YALLA_LIBRARY
}
if os.get() == "windows" then
include "win32"
else
return -- OS NOT SUPPORTED
end
The win32 dir is defined in Premake as follow:
files {
"event-loop.cpp",
"win32-exception.cpp",
"win32-exception.hpp",
"win32-window.cpp",
"win32-window.hpp",
"window.cpp"
}
And finally at the root dir I have the following Premake file:
PROJECT_NAME = "Yalla"
-- Sets global constants that represents the projects' names
YALLA_LAUNCHER = PROJECT_NAME .. ".Launcher"
YALLA_LIBRARY = PROJECT_NAME .. ".Library"
YALLA_ABS_PLATFORM = PROJECT_NAME .. ".AbstractPlatform"
YALLA_PLATFORM = PROJECT_NAME .. ".Platform"
workspace(PROJECT_NAME)
configurations { "Release", "Debug" }
flags { "Unicode" }
startproject ( YALLA_LAUNCHER )
location ( "../lua_build" )
include "src/launcher"
include "src/library"
include "src/platform"
I'm probably misunderstanding how Premake works due to lack of experience with it.
I solved it by creating a new global function and named it includedeps.
function includedeps(workspace, ...)
local workspace = premake.global.getWorkspace(workspace)
local args = { ... }
local args_count = select("#", ...)
local func = select(args_count, ...)
if type(func) == "function" then
args_count = args_count - 1
args = table.remove(args, args_count)
else
func = nil
end
for i = 1, args_count do
local projectName = select(i, ...)
local project = premake.workspace.findproject(workspace, projectName)
if project then
local topIncludeDir, dirs = path.getdirectory(project.script)
if func then
dirs = func(topIncludeDir)
else
dirs = os.matchdirs(topIncludeDir .. "/**")
table.insert(dirs, topIncludeDir)
end
includedirs(dirs)
if premake.project.iscpp(project) then
libdirs(dirs)
end
links(args)
else
error(string.format("project '%s' does not exist.", projectName), 3)
end
end
end
Usage:
includedeps(PROJECT_NAME, YALLA_LIBRARY)
or
includedeps(PROJECT_NAME, YALLA_PLATFORM, function(topIncludeDir)
return { path.join(topIncludeDir, "win32") }
end)
Update:
For this to work properly you need to make sure that when you include the dependencies they are included by their dependency order and not by the order of the directory structure.
So for example if I have the following dependency graph launcher --> platform --> library then I'll have to include them in the following order.
include "src/library"
include "src/platform"
include "src/launcher"
As opposed to the directory structure that in my case is as follow:
src/launcher
src/library
src/platform
If you will include them by their directory structure it will fail and tell you that "The project 'Yalla.Platform' does not exist."