No __init__.py, but still considered a package? - package

A foobar package
foobar
__init__.py
foo.py
bar
bar.py
Inside the __init__.py
from . import foo
from . import bar
Even though bar is not a package or a sub-package, it is still imported as a module (lolwut). I checked the import type by doing print(type(bar)) inside the __init__.py and it printed <class 'module'>... that's it. What's going on here? It is a module object, so I did print(dir(bar)) and the output was ['__doc__', '__loader__', '__name__', '__package__', '__path__', '__spec__']. Now, what's even more confusing to me is the __path__ variable in this. Isn't that a package-only thing?
Is this what's known as a namespace-package? I am thinking that it isn't, nevertheless I tried one more thing inside that __init__.py file - added a line import bar.bar. It ended in an ImportError. So, to sum up my question, what is this module useful for? Why did Python import this in the first place?
There's an amazing tutorial on this entire topic by David Beazley. I have watched the whole thing a while ago, but I guess I should watch it again to recollect everything.

From the docs
A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. Within a module, the module’s name (as a string) is available as the value of the global variable __name__.
...
Packages are a way of structuring Python’s module namespace by using “dotted module names”. For example, the module name A.B designates a submodule named B in a package named A.
...
Packages support one more special attribute, __path__. This is initialized to be a list containing the name of the directory holding the package’s __init__.py before the code in that file is executed. This variable can be modified; doing so affects future searches for modules and subpackages contained in the package.
While this feature is not often needed, it can be used to extend the set of modules found in a package.
So yes, the __path__ variable is applied to the modules inside a package as well and those modules are treated as 'submodules of a package'
Edit
In Python 2.x, that package with the from . import bar would return an ImportError at that line. On Python 3.x the modulefoobar.foo has a type of <module 'foobar.foo' from '../py/foobar/foo.py'> and the foobar.bar of <module 'foobar.bar' (namespace)>.
Apparently, it appeared here
Regular packages will continue to have an init.py and will reside in a single directory. Namespace packages cannot contain an init.py

Related

Sass #use does not work in CSS modules in React, while import does

I have generated a React project with create-react-app using the TypeScript template. I've installed Sass via npm install sass, while CSS modules were already supported by the generated template.
I've created a common set of variables within the styles/common.scss file. I wanted to import and use the variables from the file in the stylesheet of my component. The recommended way to do this seems to be the #use directive, but I was unable to get to work.
Relevant files within my project structure:
components/
custom/
Custom.module.scss
Custom.tsx
styles/
common.scss
In this scenario components/custom/Custom.module.scss tries to import and use a variable from styles/common.scss with #use "styles/common.scss". While I don't get any specific errors related to the #use directive, attempting to reference any variables from the common stylesheet results in the following error: SassError: Undefined variable.
Things I've tried so far:
Proceeding the path with ~: #use "~styles/common.scss" yields the same error.
Different combinations of adding underscore to the common stylesheet, as well as removing extensions from the path in #use: same results.
Replacing #use with #import "styles/common.scss" works as expected: the variables are available in the CSS module.
However, since the #import is set to be deprecated, I'm wondering how to get #use to work and what is the root of the issue. Note that CSS modules themselves work as expected, and I'm able to import them correctly within the tsx files.
Given a default create-react-app setup, the correct way to import a module via #use is to omit the file extension (.scss) and the proceeding underscore. So, for example, styles/_colors.scss can be imported with #use "styles/colors".
However, by default each variable (etc.) from the imported file will require a prefix to be used. So, for example, to reference $pink from the imported _colors.scss file, you'd have to write colors.$pink. To change the prefix, you can choose an alias with as in the #use directive. For example, #use "styles/colors" as c; would allow the variables to be accessed with a c. prefix. To remove the prefix altogether, use the * alias.
Additionally, adding an _index.scss file allows using entire folders with specifying individual files. Use #forward directive in the _index.scss to choose which modules are available when importing the folder.
So, to sum it up, the file structure should look roughly like this:
styles/_colors.scss
$pink: #ff00ff;
styles/_index.scss: optional, allows importing multiple modules in 1 step.
#forward "styles/colors";
components/custom/Custom.module.css
#use "styles" as *;
// Or: #use "styles/colors" as *;
// Now $pink can be referenced without an alias.

What is the correct usage of the mixin classs for TCL language?

Im attempting to update an old version of the selenium-tcl package to work with the new W3C WebDriver (or Selenium 4.0).
Original packages uses a few mixins for the webdriver class.
So I modeled what I saw and created a mixin file named mixin_action_chains.tcl [1] which has a mixin class called Mixin_Action_Chains.
Whenever I attempt to use it I get the error:
% package require selenium
::selenium::Mixin_Action_Chains does not refer to an object
Im not sure why I've modeled it pretty much exactly as I have seen in the other files such as mixin_for_scrolling.tcl [2]* file. What am I missing.
Here is the entire GitHub Repo
Im not sure what else must be done for TclOO. Any thoughts.
Thanks
Im not sure what else must be done for TclOO. Any thoughts.
Update
pkgIndex.tcl: The placement of the mixin-defining script mixin_action_chains.tcl is wrong, it comes after the mixin has already been required in the previously sourced script webdriver.tcl, like entering directly:
% oo::class create C {
mixin ::not::defined
}
::not::defined does not refer to an object
You need to change the order of source command calls in the package ifneeded script.
For the records
Still, in the original version, there were unbalanced curly braces and brackets in your script, which broke sourcing of the file for me:
https://github.com/SavSanta/w3cselenium-tcl/pull/1

dart path dependencies not working (across multiple projects)

I was attempting to make a "library" type of project in dart and then "depend" on that library from another project (all using the path dependency functionality of the yaml file). I understand that I might be able to get the dependency stuff to work if I hosted my library or if I used GIT, but I don't want to do either, because I feel that pure filesystem based dependencies should be a "no brainer".
So, without further adieu, here is my situation. I have a very simple dart library/project based on web_ui that contains two files:
esrvdartui.dart
---------------
library esrvdartui;
import 'dart:html';
import 'package:web_ui/web_ui.dart';
part 'esrvradiobutton.dart';
esrvradiobutton.dart
--------------------
part of esrvdartui;
class ESrvRadioButton extends RadioButtonInputElement
{
ESrvRadioButton ()
{
}
}
I then created another very small/simple web_ui based project called "ExampleForm" that wants to use my esrvdartui project above. Both of these projects exist in the same directory structure. My ExampleForm project contains the following yaml file:
pubspec.yaml
------------
name: ExampleForm
description: A sample WebUI application
dependencies:
js: any
browser: any
web_ui: any
esrvdartui:
path: ../esrvdartui
No matter what I set my path to in the above yaml file, I never see my web\packages directory underneath of my ExampleForm project get updated with my files from the esrvdartui project and as such, I cannot use the files in my library using the file based dependency method, because the build fails for my ExampleForm project.
"Pub install" does not complain with the above path and it doesn't complain when I use an absolute path, so I know that "Pub install" see my dependent project. It just doesn't copy the darned files for me.
Any thoughts?
My pubspec.lock file for ExampleForm is:
# Generated by pub
# See http://pub.dartlang.org/doc/glossary.html#lockfile
{"packages":{"logging":{"version":"0.5.0+1","source":"hosted","description":"logging"},"source_maps":{"version":"0.5.0+1","source":"hosted","description":"source_maps"},"unittest":{"version":"0.5.0+1","source":"hosted","description":"unittest"},"pathos":{"version":"0.5.0+1","source":"hosted","description":"pathos"},"analyzer_experimental":{"version":"0.4.7+1","source":"hosted","description":"analyzer_experimental"},"web_ui":{"version":"0.4.6+1","source":"hosted","description":"web_ui"},"js":{"version":"0.0.21","source":"hosted","description":"js"},"csslib":{"version":"0.4.3","source":"hosted","description":"csslib"},"esrvdartui":{"version":"0.0.0","source":"path","description":{"relative":false,"path":"C:/Users/Jason/dart/esrvdartui"}},"html5lib":{"version":"0.4.3","source":"hosted","description":"html5lib"},"args":{"version":"0.5.0+1","source":"hosted","description":"args"},"browser":{"version":"0.5.0+1","source":"hosted","description":"browser"},"meta":{"version":"0.5.0+1","source":"hosted","description":"meta"}}}
My pubspec.lock file for esrvdartui is:
Generated by pub
See http://pub.dartlang.org/doc/glossary.html#lockfile
{"packages":{"meta":"version":"0.5.0+1","source":"hosted","description":"meta"},"browser":{"version":"0.5.0+1","source":"hosted","description":"browser"},"args":{"version":"0.5.0+1","source":"hosted","description":"args"},"html5lib":{"version":"0.4.3","source":"hosted","description":"html5lib"},"analyzer_experimental":{"version":"0.5.0+1","source":"hosted","description":"analyzer_experimental"},"csslib":{"version":"0.4.3","source":"hosted","description":"csslib"},"web_ui":{"version":"0.4.6+1","source":"hosted","description":"web_ui"},"pathos":{"version":"0.5.0+1","source":"hosted","description":"pathos"},"js":{"version":"0.0.22","source":"hosted","description":"js"},"source_maps":{"version":"0.5.0+1","source":"hosted","description":"source_maps"},"unittest":{"version":"0.5.0+1","source":"hosted","description":"unittest"},"logging":{"version":"0.5.0+1","source":"hosted","description":"logging"}}}
I finally got this to work, but for the life of me, I couldn't find this documented anywhere. All you have to do is create a project in the Dart IDE. Then, create a top level folder in that project called "lib" (blow all other directories away other than the top level "packages" folder). Now, create your main library's .dart file. Let's call it "mylibrary.dart". This contents of this file will look something like this:
mylibrary.dart
library mylibrary;
import 'dart:json';
part 'src/libraryfile1.dart';
Now, create a sub-directory underneath of "lib" to place your library's source files into. This can really be named anything, but I choose to name it "src". Place your libraryfile1.dart file there and it should look something like this:
src/libraryfile1.dart
part of hix_lib;
.
.
.
All import statements should always be placed in your top-level main library file: mylibrary.dart.
Now, in the project that you wish to use this file-based library in, you must add your "mylibrary" to your project's pubspec.yaml file and choose: "Source: path". On my machine, because all projects are in the same directory, my path simply points to: ../mylibrary
And that's all there is to do!!!!!

CakePHP - Include class from a directory outside the app directory

I am trying to include a miscellaneous library class from outside my app (it gets used by different apps).
My app is located at:
/var/www/websites/my_website/app/
And the class is located at:
/var/www/websites/libs/CakePHP/MyClass.php
In my bootstrap I'm struggling to figure out how to add the path for loading the classes from that directory:
App::build(array('Lib' => array('/var/www/websites/lib/')));
App::uses('MyClass', 'CakePHP');
$myClass = new MyClass();
Loading shouldn't be done in your bootstrap, but in your AppController's beforeFilter method instead.
Also, there is a reserved place for non-Cake libraries, being the app/Vendor directory. You can place all your classes in there and then load team easily with:
App::uses('MyClass', 'Vendor');
If it really needs to be in an alternative path, you need to specify and call the full path instead. And make sure to use the same names. Right now, you're specifying Lib, yet calling CakePHP as if that was a build by itself (which it's not). This won't work. It should look like this instead:
App::build(array('Lib' => array('/var/www/websites/lib')));
App::uses('MyClass', 'Lib/CakePHP'); // Define the subdirectory here
Also check the documentation on loading vendor files, it has quite some examples.

GWT Compile Error - Restlet related?

The XXX are just names that I need to keep confidential.
Compiling module com.XXX.XXX.XXX_Test
Validating newly compiled units
Ignored 12 units with compilation errors in first pass.
Compile with -strict or with -logLevel set to TRACE or DEBUG to see all errors.
Scanning for additional dependencies: file:/D:/Eclipse/Indigo/Workspace/XXX%20Test/src/com/XXX/XXX/client/Restlet.java
Computing all possible rebind results for 'com.wai.XXX.client.proxy.DonglesProxy'
Rebinding com.XXX.XXX.client.proxy.DonglesProxy
Checking rule <generate-with class='org.restlet.rebind.ClientProxyGenerator'/>
[ERROR] Errors in 'file:/D:/Eclipse/Indigo/Workspace/XXX%20Test/src/com/XXX/XXX/client/proxy/DonglesProxy.java'
[ERROR] Line 11: No source code is available for type org.restlet.resource.ClientProxy; did you forget to inherit a required module?
[ERROR] Unable to find type 'com.XXX.XXX.client.proxy.DonglesProxy'
[ERROR] Hint: Previous compiler errors may have made this type unavailable
[ERROR] Hint: Check the inheritance chain from your module; it may not be inheriting a required module or a module may not be adding its source path entries properly
Here is what the console tells me when I try to do a GWT Compile on my project. What I don't understand that I've included the 'org.restlet.jar' into the buildpath of the project and have the following imports in the DonglesProxy source code:
import org.restlet.resource.ClientProxy;
import org.restlet.resource.Put;
Anyone any ideas?
I'm new to Java and the whole Web Application process so my knowledge is a bit lacking. This is actually someone else's project that has been left unfinished so I'm trying to debug/understand someone else code whilst learning as I go along...nightmare :(
Any help would be greatly appreciated!
Cheers
Kev
You must use :
import org.restlet.client.resource.ClientProxy;
import org.restlet.client.resource.Get;
import org.restlet.client.resource.Post;
import org.restlet.client.resource.Result;
to define the proxies; Take notice of the "client" part;
The referenced imports must be part of the GWT distribution;
You have to add it to your projects xml file. That is to com.XXX.XXX.XXX_Test.gwt.xml or something like that.
You have to add this line there,
<inherits name='org.restlet.whateverClass.xmlfilename />'
This means you are pointing to the xml file named xmlfilename.xml at the path org.restlet.whteverclass
So say for example if i am using sencha ui libraries jar, I will add,
<inherits name='com.sencha.gxt.ui.GXT' />
So here there will should be a xml file called GXT.xml at the path 'com.sencha.gxt.ui'

Resources