Playwright resolve relative path outside test directory - playwright

I have this problematic import.
import * as Collection from '../../lib/collection.js'
root
> lib
>> collection.js
> tests
>> folder1
>>> collection.spec.js
how can resolve this relative path with something like
import * as Collection from '#/lib/collection.js'
OR
import * as Collection from 'lib/collection.js'
tried both. neither worked.
any ideas?

I’m not sure what you mean when you say “problematic”, as that import looks correct/like it should work fine. Is it not working for you?
If your concern is that you’d rather avoid having to go up directories with ../ leading to the two options you tried, and you’re trying to get something like that to work, that’s addressed better than I could elsewhere such as this accepted answer about what # does and the others. Short answers:
lib/collection.js isn’t relative, and would refer to a package (see also Node’s doc on import specifiers
#/lib/collection.js isn’t a natively supported JavaScript syntax and requires a module loader/bundler, but should work once set up.

Related

Importing python packages within python packages (relative imports, __init__.py)

Let's say I have a directory like this
In main.py I have the code from main_package import func but I get an error saying
test/main_package/func.py", line 1, in <module>
from sub_package.aa import aaf1
ModuleNotFoundError: No module named 'sub_package'
The line from sub_pacakage.aa import aaf1 in main_package/func.py is causing the error. I can switch that line to an asbolute path (from main_package.sub_pacakage.aa import aaf1) and it works fine.
However, I want to not change the relative path to an absolute path.
I am assuming I have to modify main_package/__init__.py to achieve this but I am not sure how. How do I go about this?
I've tried adding from . import sub_package to main_package/__init__.py but it doesn't seem to be working.
The issue with absolute Python imports is that they are actually relative to any path defined in sys.path and also to your current working directory.
Thus, to make an import like from sub_package.aa work, you need to ensure that the parent folder of "sub_package" is in sys.path or is your current working directory.
The same goes for from main_package.sub_package.aa. You need to make sure that the parent folder of main_package is in sys.path or is your current working directory. So it makes a different how and from which directory you execute your Python code. You usually need to "cd" to the right directory first.
I've had similar problems in the past and thus I've created an experimental, new import library: ultraimport
It gives you more control over your import and lets you do file system based imports and therefore real relative imports. You could then write your func.py like this:
import ultraimport
# Given that aaf1 is defined in aa.py from your sub package
aaf1 = ultraimport('__dir__/subpackage/aa.py', 'aaf1')
This will always work, no matter what is your current working directory, what is your sys.path or how you run your code.

correct way to import mydart.dart file in main

In my dart project projectxyz, I have a dart class declared in in myclass.dart. In main.dart, Android Studio gives two ways, both work, but I did not understand what are the pros and cons of each method:
import 'myclass.dart';
or:
import 'package:projectxyz/myclass.dart';
What is the difference in these two approaches?
That depends on how the main file itself is invoked (and where it's located).
I'll assume the main.dart library is inside the lib/ directory, because otherwise you wouldn't have the two options for importing myclass.dart.
If you invoke the main file with a file: URI, then the relative import of myclass.dart will also be imported with a file: URI. Since Dart uses the import URI to distinguish different libraries, if someone else imports myclass.dart using a package: URI, then it will be treated as two different libraries introducing different classes with the same name.
It used to be that running dart lib/main.dart would treat that as a file: URI. The Dart parser has gotten smarter about that, and now it recognizes that an entry point library in a lib/ directory should have been a package: URI, and replaces the entry point URI with package:projectxyz/main.dart.
After that, it makes no difference whether you use myclass.dart or package:projectxyz/myclass.dart.
Really, there is no difference between the two. Saying import 'myclass.dart' is high-level sugar for import 'package:projectxyz/myclass.dart';.
On the other hand, import 'myclass.dart' is easier to read and understand, and generally looks better. It also decreases confusion as to where exactly your code is being imported from, as anybody who reads this statement knows to look for the file elsewhere in your project. Because of this, you should try to use this form wherever possible.

How to check object class type in Dart

In my code, there is a place where I need to take different actions based on the input class type.
So I write two lines to check an input object's class type.
debugPrint("Let me know the next action: $action");
debugPrint((action is LoadPomodorosAction).toString());
And the output is
I/flutter (24128): Let me know the next action: Instance of 'LoadPomodorosAction'
I/flutter (24128): false
What does this mean?
The object 'action' is "Instance of 'LoadPomodorosAction'" and at the same time its class type is not LoadPomodorosAction .
how do I adjust my code so that I can know the class type of action?
I was suspecting that maybe there is something wrong with runtimetype. But how do I get to know the runtimetype?
I've tried replicating your issue and I'm not able to reproduce it. But to explain your inquiry, here is a complete details about the difference between the relative path and absolute path when used in imports as discussed in this SO post:
package imports
'package:... imports work from everywhere to import files from
lib/*.
relative imports
Relative imports are always relative to the importing file. If
lib/model/test.dart imports 'example.dart', it imports
lib/model/example.dart.
If you want to import test/model_tests/fixture.dart from any file
within test/*, you can only use relative imports because package
imports always assume lib/.
This also applies for all other non-lib/ top-level directories like
drive_test/, example/, tool/, ...
lib/main.dart
There is currently a known issue with entry-point files in lib/*
like lib/main.dart in Flutter.
https://github.com/dart-lang/sdk/issues/33076
Dart always assumed entry-point files to be in other top-level
directories then lib/ (like bin/, web/, tool/, example/,
...). Flutter broke this assumption. Therefore you currently must not
use relative imports in entry-point files inside lib/
See also
How to reference another file in Dart?
Previously, this bug was posted in GitHub as an issue between relative and absolute path. It seems that this was resolved per this GitHub post.

I see an angular2 'bind' function defined in angular2/angular2.d.ts - did it used to be in 'angular2/di.d.ts?

Many of the samples I have seen for angular2 have the following import statement:
import {bind} from 'angular2/di';
I am working in VS Code (with TypeScript) and it complains about not being able to find the angular2/di module.
However I do see a bind function defined in angular2/angular2.d.ts. If I change the import statement to the following, then the error goes away.
import {bind} from 'angular2/angular2';
Is the question in the title off-base and I am making some erroneous assumption?
If not, why do many samples reference one module to import the bind function from, yet I seem to be able to get it from a different module?
Most likely because you looked at versions from older alphas. Look at the angular2.ts file. Everything is exported from it. Also note that the d.ts is going to contain everything to resolve types in your IDE and at compilation time. What import does is actually importing the .js files.

update module on import to python interpreter

In short
How to force python interpreter to load the most up-to-date code version of my module everytime I make some changes in the module code?
Or at least reload the last modified version by typing
>>> from myModule import *
into console, without necessity to restart whole python console and setup everything again and again anytime I make some changes? This is extremely unpleasant behavior for debugging.
--------- LONGER STORY -----------
I tried to delete the .pyc file, and import it again - but it has no effect. It does't even create .pyc file again - so I expect it completely ignore my "import" command if the module is already loaded.
this also does not help:
>>> mymodule.myfunc() # the old version
>>> del myModule # unload mymodle from python conole / interpeter
... # now I removed .pyc
... # now I make some modifications in mymodule.myfunc() code
>>> mymodule.myfunc() # module is unknonwn, ... OK
>>> import myModule # try to load modified version
>>> mymodule.myfunc() # stil the old version :(((((, How it can remember?
I have tried also Spyder where is this feature called "User Module Deleter (UMD)"
http://pythonhosted.org/spyder/console.html#reloading-modules-the-user-module-deleter-umd
which I thought should do exactly this, but it seem it doesn't (Yes, I checked that it is turned on).
Maybe I'm missing something - can somebody explain me how is it supposed to be used?
Is this somehow affected by the fact that the imported module is not in "Working directory" but in PYTHONPATH ?
(Spyder dev here) I think at the moment you are not able to reload a module directly in the console (but we are considering to change this in the future).
The idea about UMD is that it will reload your modules but only if you run a file from the editor that imports them. It doesn't work if you want to reload them directly in the console.
Let's say you developed a module, then you are probably using it in a different script that (most likely) you'll be writing in our editor and send it to run to our console. UMD is a little bit of magic that reloads it for you when that happens.
Maybe useful for others. In Spyder 3.0.0, the modules can be reloaded by,
Tools -> Update modules names list.
It worked for me.

Resources