I am trying to replicate a namespace kind of pattern in D. I would like to have one class per file and be able to use a single import statement to include all classes from a group of files.
in my root folder I have moduleExp.d
import foo.module;
import std.stdio;
void main()
{
writeln("hello world");
auto quakk = new Quakk();
quakk.write();
}
In my foo folder I have module1.d:
module foo.module1;
import std.stdio;
class Quakk
{
public string name = "thename";
public void write()
{
writeln(this.name);
}
}
Also in foo folder, I have module.d which I thought I could use to publicly import all my class modules into so they can be used in moduleExp.d of the root folder.
module foo.module;
public import foo.module1;
When I try to compile on Windows from the root folder with rdmd moduleExp, I get errors:
moduleExp.d(1): Error: identifier expected following package
moduleExp.d(1): Error: ';' expected
Failed: ["dmd", "-v", "-o-", "moduleExp.d", "-I."]
I'm not sure what I'm doing wrong here. if I change line 1 in module.Exp.d to import foo.module1; then everything works.
module is a reserved keyword that you shouldn't use as a module name. And what you are trying to do exists as a feature in dlang called package modules. Look in http://dlang.org/module.html for documentation.
So basically, you have a module under the folder foo, its name should be package.d, where you publicly import the modules within foo and any other modules that you want. Example:
module foo; // notice the module name is used
// for the package module and not foo.package
public import foo.module1;
public import foo.module2;
...
And now when you want to import the package, just write import foo.
Related
I am trying to understand how to import code from one file to another. I have two files file1.py and file2.py. I am running code in the first file, and have many variables and functions defined in the second file. I am using from file2 import * to import the code into file1.py. I have no problem using variables defined in file2.py in file1.py, but with functions I am getting NameError: name 'myfunc' is not defined when I try to use the function in file1.py. I can fix this problem by writing from file2 import myfunc, but I thought writing * would import everything from that file. What is the difference for functions versus variables?
I have tried to recreate the setup you have described and it is working OK for me. Hopefully this will give you an idea of how to get it to work.
# file1.py #####################################
import sys
sys.path.append("/home/neko/test/")
import file2
if __name__ == "__main__":
file2.testfunc()
# file2.py ######################################
testvar = 'hello'
def testfunc(): print testvar
For this test I was using python version 2.6.6
Both file1.py and file2.py is in /home/neko/test/
I have a flask application that I would like to convert into an executable for deploying elsewhere. I have used py2exe for that. I am getting jinja2:TemplateNotFound error. I have copied the static and templates folders into the dist folder where the exe files reside. Pls let me know if I am missing something. My setup file is as follows:
from distutils.core import setup
import py2exe
import os
from glob import glob
import sys
from distutils.filelist import findall
import matplotlib
matplotlibdatadir = matplotlib.get_data_path()
matplotli bdata = findall(matplotlibdatadir)
matplotlibdata_files = []
for f in matplotlibdata:
dirname = os.path.join('matplotlibdata', f[len(matplotlibdatadir)+1:])
matplotlibdata_files.append((os.path.split(dirname)[0], [f]))
data_files=[('static', glob("D:\\pythonLearning\\static\\*.*")), ('templates', glob("D:\\pythonLearning\\templates\\login.html"))]
data_files.extend(matplotlibdata_files)
print data_files
sys.path.append('C:\\Windows\\winsxs\\x86_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.6161_none_50934f2ebcb7eb57')
setup( console=['myfile.py'],
options={ 'py2exe': { 'packages' : ['matplotlib', 'pytz','werkzeug','email','jinja2.ext'],
'includes': ['flask','jinja2'] } },
data_files=data_files )
This is because jinja expects your egg to be unzipped and available via the filepath. See for more information.
You can workaround this for typical data files using this
but jinja2 has no direct support for this and you would have to implement this yourself
I want to use test-framework-th for generating test groups.
I have this main test module:
module Main where
import Test.Framework.TH
import Foo.Test
main = $(defaultMainGenerator)
And this module containing the test:
module Foo.Test where
import Test.HUnit
case_1 = do 1 #=? 2
However, defaultMainGenerator does not detect the test in the Foo.Test module. It only detects tests in the module in which it is called (in this case Main).
How can I split my tests across modules without duplicating boilerplate for every test?
Use testGroupGenerator in each module to generate a test group for that module, then use those from main, e.g.
Foo.hs:
module Foo where
import Test.Framework.TH
tests = $(testGroupGenerator)
...
Bar.hs:
module Bar where
import Test.Framework.TH
tests = $(testGroupGenerator)
...
Main.hs:
import Test.Framework
import Foo
import Bar
main = defaultMain [Foo.tests, Bar.tests]
I'm trying to define my custom field option in google protocol buffers. If I create such a file, everything works ok:
import "google/protobuf/descriptor.proto";
package tutorial;
extend google.protobuf.FieldOptions {
optional int32 myopt = 70000;
}
message Persona {
required string name = 1 [(myopt)=5];
}
However, if I try to move "myopt" definition to another file, compilation fails:
myext.proto:
package myext;
import "google/protobuf/descriptor.proto";
extend google.protobuf.FieldOptions {
optional int32 myopt = 70000;
}
addressbook.proto:
import "google/protobuf/descriptor.proto";
import "myext.proto";
package tutorial;
message Persona {
required string name = 1 [(myopt)=5];
}
compilation:
$ protoc --cpp_out=. -I/usr/include -I. addressbook.proto
addressbook.proto:8:29: Option "(myopt)" unknown.
Is there any way to define custom field options in other file than the one that use it? It is important to move common part to a common file if I want to use my option in several .proto files.
Because you have a package myext
you should be doing
import "myext/myext.proto";
with myext.proto located in a sub-directory of myext.
In protocol buffer package indicates the directory where the file should reside (like in java)
Because you've made a new package with your new proto file, you'll need to reference the package's namespace.
As you noted in your comment, just use "(myext.myopt)" instead of "(myopt)", so it looks like this:
myext.proto:
Unmodified from what you showed
package myext;
import "google/protobuf/descriptor.proto";
extend google.protobuf.FieldOptions {
optional int32 myopt = 70000;
}
addressbook.proto:
Replace "(myopt)" with "(myext.myopt)"
import "google/protobuf/descriptor.proto";
import "myext.proto";
package tutorial;
message Persona {
required string name = 1 [(myext.myopt)=5];
}
Suppose I have the following directory structure for the project:
myproj/dir1/file1.d
myproj/dir2/file2.d
myproj/main.d
How can I import main and file2 modules within the source file file1.d?
file1.d will have module dir1.file1; line , file2.d will have module dir2.file2; line and main.d will start with module main;.
Module declarations above will tell D what to do when it encounters line like: import main, dir2.file2;;
As suggested by #sigod , read the http://dlang.org/module.html for more information.