Scalatest: Iterable should not contain any of the following set? - scalatest

Currently in ScalaTest, we can do
Set(1,2,3,4) should not contain (5)
Set(1,2,3,4) should not contain (6)
What would be good ScalaTest style to do:
Set(1,2,3,4) should not contain any of Set(5,6)
Currently, I can only think of
Set(1,2,3,4) & Set(5,6) should be ('empty)

In Scalatest 1.x you can only use something like:
Set(1,2,3,4) should not (contain (5) or contain(6))
or
set2.foreach(set1 should not contain _)
but there's no elegant way to express that having two sets, they should not intersect.
In Scalatest 2.0 (which is in RC1 state at the moment) you can tweak above version a bit as:
Set(1,2,3,4) should contain noneOf (5, 6)
but still you cannot use a value of type Set and you have to give the elements directly.

Related

Interconversion and difference

I have two types of tensors.
1) <class 'tensorflow.python.ops.resource_variable_ops.ResourceVariable'>
2) <class 'tensorflow.python.framework.ops.Tensor'>
What is the difference between the two? Can interconversion be done? In particular if I want to convert the second type to first type.
Thanks
A tf.Tensor is basically a multidimensional array of elements, while a tf.ResourceVariable is pretty much like a fancier tf.Variable. I think they were used in Tensorflow 1, but in Tf2 got removed, I can't even find them in the documentation (are you using tf1?).
Let's just talk about variables, that can be used similarly. The tf.Variable is a wrapper around a tensor, so you can initialize the variable using the tensor, as we will see below.
Having a tf.Variable (here to learn more) adds further capabilities to the tensor. An important distinction is that the tf.Variable maintains state across multiple runs. The variable is pretty much used to represent the trainable parameters of the model, while the tensor is used as array to feed to the network.
So as I was saying, a tf.Variable stores a persistent tensor and can be initialized using that tf.Tensor like this:
variable = tf.Variable(my_tensor)

Display data in 2d list in python

For an assignment in a my python class. I have to take the following list data:
animal = [
('cat', 'meow', 4),
('dog', 'bark', 10),
('bird', 'chirp', 0.5),
('snake', 'hiss', 3),
('cow', 'moo', 250),
('lion', 'roar', 500)
]
And create nested loop statements to display the following:
[1]: https://i.stack.imgur.com/KpPL4.png
I think it won't help you learn if we give you the complete answer, but here are some things that can help:
Do you know that each of those structures (the array, and the tuples in the array) is an iterable? That means you can loop over them without using indexes, the way one might in another language. Check out that link to get some ideas about how you could nest an iterable for your individual animals inside the iterable for the whole list.
If you're using Python 3.6 or newer, you can use f strings for your formatting, which will give you a tidy way to print the values with flexible spacing, so they all line up the way you'd like. There are good examples of the older format for formatting strings with whitespace padding in this SO question: Python spacing and aligning strings
I think you should be able to figure out your answer based on those two pointers.

AngularJS arrays, proto and .length - Why can't i get a valid number?

Sometimes you need to ng-if or ng-show an item in html based on some choices made earlier. One of these for me is "Additional Item". You can enter one set of information, and also if you want, an additional set. This creates an array of 2 similar objects. With this setup, you can only have 1 or 2 objects in this array. (important, since the scope of this question needs to be limited this way)
I want to ng-show an html directive based on "myItemsArray.length > 1". Since the array can (read should) only be 1 or 2 in length (not 0), this should work. However, it does not, because AngularJS seems to be adding an item "proto" to the array which adds to the count. See the image.
The problem is, proto makes the array length equal 2. I am not going to just look for length > 2 because i really don't know if i can count on proto always being there, and i just think thats bad practice anyway.
Also, i know there are MANY other ways of doing this (setting a boolean, or using another var to indicate etc, but i really just want to work with count of items in the array because "business logic"..
EDIT:
After doing a little debugging, i'm seeing that i have an array of "Object, undefined". How is this even possible :)
Some search lead me to this. Why are some values ​​in my array undefined
EDIT:
Seems that using a delete may cause this problem

Gremlin / Bulbflow: How to select nodes based on their edges and related vertice's properties

Sorry for the long post, but I want to avoid any misunderstanding about what I'm looking for :)
I am currently discovering graph databases, and experimenting a bit with bulbflow/neo4j.
Thus, I am trying to use gremlin for most of my requests, but I do not know if the request I want is even feasible or not. I may even be wrong about trying to use a graph db for such a use case, so don't mind telling me whether you think I'm on the right path or not.
First, let me provide a bit of context:
I work on an early-stage open-source project, which is a compiler for a DSL language generating C code. We are currently planning to re-write the whole thing in python for many many reasons (the language, re-designing, opening to a community and such...). The compiler includes what I'll call a cache of the compiled interfaces and templates. The interfaces describe the templates, and each template is associated to a configuration (a list of typed values associated to variables described by the interfaces).
The aim of the request I'm wishing to build is to select a single template implementation depending on an input configuration (actually used in the generation mechanism of the compiler). In the end, I want to be able to request directly through gremlin (if possible at all) a single element I'm looking for in order to provide unicity for the elements that can be found within this "cache". Currently, I manually match this configuration in the python code, but I want to know if it is feasible to do it directly within gremlin.
-
So let's define a sample graph for my use-case:
We have three types of vertices:
Def (Definition), contains a String property called "signature", which is actually the signature of the template defined by this node.
Impl (Implementation), containing two properties which are pathes to the original source and pre-compiled files.
Var (variable), containing a String property which is the signature of the variable.
Then, a few kind of edges:
Def -> impl_by -> Impl (multiple implementations can exist for a definition, does not contain any property)
Impl -> select_by -> Var (Implementations may be selected through a constraint over a configuration variable's value, each edge of this type contains actually three properties: type, value, and constraint - a comparison operator -)
The selected_by edge (or relationship, following bulflow's vocabulary) describes a selection constraint, and thus has the following properties:
val (value associated to the variable for the origin implementation)
op (comparison operator telling which kind of comparison to make for the constraint to be valid or not)
This translates as a graph such as (I'll omit the types from the selected_by edges in this graph):
-- select_by { value="John", op="="} ---------
| \
(1)--Impl--- select_by { value=12, op=">"} ------ \
| \ \
| \ |- Var("name")
| |- select_by { value="Peter", op="="} -----------/
Def (2)--Impl-- \/
| |- select_by { value=15, op="<"} ---- /\
| \ / \
| |-/----|--- Var("ver")
(3)--Impl--- select_by { value="Kat", op="!="} ------/ /
| /
|--- select_by { value=9, op=">"} ---------/
What I want to do is to select one (or more) Impl depending on their relationship with the Vars. Let's say I have a configuration as follows:
Config 1:
variable="name", value="Peter"
variable="ver", value=16
This would select Impl(3) Since Peter != Kat AND 16 > 9, but not Impl(1) since Peter != John nor Impl(2) since 16 !< 15.
I was blocked on multiple levels, so I was starting to wonder if this was even feasible:
I could not find how to give such arguments (the configuration) to a gremlin script
I could not find how to select the Impl based on conditions over the outgoing edges.
I hope this wasn't too confusing.
Cheers, and thanks !
EDIT:
I managed to make part of my request work, by using repeatedly backtracking and filters. The request (X being the starting vertex, VALUE the value I want to match, and NAME the name of the variable to be matched) looks like this:
Basis of the request:
g.v(X).out('impl').as('implem')
Repeat this part for each couple VALUE/NAME:
.out('select_by').filter{it.value=='VAL‌​UE'}
.inV('select_by').filter{it.name=='NAME'}
.back('implem')
The only thing currently missing is that I do not know how to use the select_by edge's 'op' property to determine how to build the filter to use. For instance, thre are cases where I want to match exactly the configuration (and thus, as in this request, I ignore the 'op' property), but there are cases where I want to take the 'op' property into account, and use the associated comparator in the filters.
Is there any way to do that ? (Or should I post another question?)

Is there YAML syntax for sharing part of a list or map?

So, I know I can do something like this:
sitelist: &sites
- www.foo.com
- www.bar.com
anotherlist: *sites
And have sitelist and anotherlist both contain www.foo.com and www.bar.com. However, what I really want is for anotherlist to also contain www.baz.com, without having to repeat www.foo.com and www.baz.com.
Doing this gives me a syntax error in the YAML parser:
sitelist: &sites
- www.foo.com
- www.bar.com
anotherlist: *sites
- www.baz.com
Just using anchors and aliases it doesn't seem possible to do what I want without adding another level of substructure, such as:
sitelist: &sites
- www.foo.com
- www.bar.com
anotherlist:
- *sites
- www.baz.com
Which means the consumer of this YAML file has to be aware of it.
Is there a pure YAML way of doing something like this? Or will I have to use some post-YAML processing, such as implementing variable substitution or auto-lifting of certain kinds of substructure? I'm already doing that kind of post-processing to handle a couple of other use-cases, so I'm not totally averse to it. But my YAML files are going to be written by humans, not machine generated, so I would like to minimise the number of rules that need to be memorised by my users on top of standard YAML syntax.
I'd also like to be able to do the analogous thing with maps:
namedsites: &sites
Foo: www.foo.com
Bar: www.bar.com
moresites: *sites
Baz: www.baz.com
I've had a search through the YAML spec, and couldn't find anything, so I suspect the answer is just "no you can't do this". But if anyone has any ideas that would be great.
EDIT: Since there have been no answers, I'm presuming that no one has spotted anything I haven't in the YAML spec and that this can't be done at the YAML layer. So I'm opening up the question to idea for post-processing the YAML to help with this, in case anyone finds this question in future.
The merge key type is probably what you want. It uses a special << mapping key to indicate merges, allowing an alias to a mapping (or a sequence of such aliases) to be used as an initializer to merge into a single mapping. Additionally, you can still explicitly override values, or add more that weren't present in the merge list.
It's important to note that it works with mappings, not sequences as your first example. This makes sense when you think about it, and your example looks like it probably doesn't need to be sequential anyway. Simply changing your sequence values to mapping keys should do the trick, as in the following (untested) example:
sitelist: &sites
? www.foo.com # "www.foo.com" is the key, the value is null
? www.bar.com
anotherlist:
<< : *sites # merge *sites into this mapping
? www.baz.com # add extra stuff
Some things to notice. Firstly, since << is a key, it can only be specified once per node. Secondly, when using a sequence as the value, the order is significant. This doesn't matter in the example here, since there aren't associated values, but it's worth being aware.
As the previous answers have pointed out, there is no built-in support for extending lists in YAML. I am offering yet another way to implement it yourself. Consider this:
defaults: &defaults
sites:
- www.foo.com
- www.bar.com
setup1:
<<: *defaults
sites+:
- www.baz.com
This will be processed into:
defaults:
sites:
- www.foo.com
- www.bar.com
setup1:
sites:
- www.foo.com
- www.bar.com
- www.baz.com
The idea is to merge the contents of a key ending with a '+' to the corresponding key without a '+'. I implemented this in Python and published here.
(Answering my own question in case the solution I'm using is useful for anyone who searches for this in future)
With no pure-YAML way to do this, I'm going to implement this as a "syntax transformation" sitting between the YAML parser and the code that actually uses the configuration file. So my core application doesn't have to worry at all about any human-friendly redundancy-avoidance measures, and can just act directly on the resulting structures.
The structure I'm going to use looks like this:
foo:
MERGE:
- - a
- b
- c
- - 1
- 2
- 3
Which would be transformed to the equivalent of:
foo:
- a
- b
- c
- 1
- 2
- 3
Or, with maps:
foo:
MERGE:
- fork: a
spoon: b
knife: c
- cup: 1
mug: 2
glass: 3
Would be transformed to:
foo:
fork: a
spoon: b
knife: c
cup: 1
mug: 2
glass: 3
More formally, after calling the YAML parser to get native objects from a config file, but before passing the objects to the rest of the application, my application will walk the object graph looking for mappings containing the single key MERGE. The value associated with MERGE must be either a list of lists, or a list of maps; any other substructure is an error.
In the list-of-lists case, the entire map containing MERGE will be replaced by the child lists concatenated together in the order they appeared.
In the list-of-maps case, the entire map containing MERGE will be replaced by a single map containing all of the key/value pairs in the child maps. Where there is overlap in the keys, the value from the child map occurring last in the MERGE list will be used.
The examples given above are not that useful, since you could have just written the structure you wanted directly. It's more likely to appear as:
foo:
MERGE:
- *salt
- *pepper
Allowing you to create a list or map containing everything in nodes salt and pepper being used elsewhere.
(I keep giving that foo: outer map to show that MERGE must be the only key in its mapping, which means that MERGE cannot appear as a top-level name unless there are no other top level names)
To clarify something from the two answers here, this is not supported directly in YAML for lists (but it is supported for dictionaries, see kittemon's answer).
To piggyback off of Kittemon's answer, note that you can create mappings with null values using the alternative syntax
foo:
<< : myanchor
bar:
baz:
instead of the suggested syntax
foo:
<< : myanchor
? bar
? baz
Like Kittemon's suggestion, this will allow you to use references to anchors within the mapping and avoid the sequence issue. I found myself needing to do this after discovering that the Symfony Yaml component v2.4.4 doesn't recorgnize the ? bar syntax.

Resources