0

I'm currently working on a python project that is interacting with a kdb+ database. A future version of the database will have a minor change, and since I can't proceed with my python programming, I thought I'd give it a shot myself instead of waiting for the update from the kdb developer.

I'm working with a mock database that spits out randomly generated symbols that look like this: instr1,instr2, instr81 etc.

I have looked around and beleive the symbols are generated by the line .gen.uni:$"instr",/: string til .gen.cfg.uniSize;

The thing I'd like to achieve is for the symbols to be exchangeX.instrY instead of just instrY (where X and Y are just some generated numbers).

for convieniece I've changed the line to $"instr",/: string til 4 so my brain doesn't hurt as much.

How would one achieve something like this? I found out about the sv function thus I've tried many variations of sv ($"exchange",/: string til 4;$"instr",/: string til 4) but nothing seems to work.

Thanks for your time!

PS: I've removed some backticks here and there because of SO's formatting

Thomas Smyth - Treliant
  • 4,993
  • 6
  • 25
  • 36
Karim Stekelenburg
  • 633
  • 12
  • 26

3 Answers3

1

Is this what you are looking for? I spaced it out a tad.

`$"exchange" ,/: (string til 4) ,' ".inst" ,/: string til 4
  • Hi there! Yes it sure looks like that! When I try translating it back to the generator format and put it in the generator file it does not work as expected though: ``.gen.uni:`$"exchange" ,/: (string til .gen.cfg.uniSize) ,' ".inst" ,/: string til .gen.cfg.uniSize``. Any ideas on that maybe? – Karim Stekelenburg Aug 15 '17 at 11:44
  • It looks good to me. When you said it didn't not work as expected, what were you expecting? – notlightnorchroma Aug 15 '17 at 11:52
  • Well your answer worked perfectly. The thing is that I'm trying to achieve this thing inside a mocking data generator. So instead of having a fixed `til 4` at the end, I'd like to use the `.gen.cfg.uniSize` variable that was already inside the script (again, I have not written it, just trying to achieve this so I can move on with my python code). I'm not sure what this variable is, but if I lookup the definition I find this ``.gen.cfg.uniSize: .cr.getCfgField[`THIS;`group;`cfg.uniSize];`` – Karim Stekelenburg Aug 15 '17 at 11:58
  • The thing that is most important to me at this point is that the one `instr` can have multiple `exchange`'s. For instance: `exchange3.instr8` and `exchange20.instr8`. When using `til 4` the numbers always match. That is a bit useless for my use case here. Thank you for your time, very kind ;) – Karim Stekelenburg Aug 15 '17 at 12:01
1

using the roll operator is useful for generating this kind of data: http://code.kx.com/q/ref/random/#roll

Function below will generate a random list of symbols.

paramaters:

x - number of symbols you want generated

y - number of different exchanges

z - number of different instruments

q){` sv' flip `$("exch",/:string x?y;"inst",/:string x?z)}


q){` sv' flip `$("exch",/:string x?y;"inst",/:string x?z)}[100;4;10]
`exch0.inst2`exch3.inst3`exch3.inst0`exch2.inst0`exch3.inst4`exch3.inst4`exch2.inst0`exch0.inst3`exch3.inst4`exch0.inst5`exch3.inst0`exch1.inst5`exch1.inst7`exch2.inst4`exch2.inst3`exch3.inst1`exch3.inst6`exch2.inst2`exch2.inst..
q)
emc211
  • 1,369
  • 7
  • 14
0

Adverbs remove some repetition. For 100 examples of any of 10 instruments from any of 4 exchanges:

"."sv/:flip("exchange";"inst"),/:'string 100?/:4 10

To break that down a little: 100?/:4 10 returns two 100-vectors, one drawn from 0-3, the other from 0-99, courtesy of the /: (each-right) adverb. string casts them to strings. Let’s call that n.

q)show n:string 100?/:4 10
,"3" ,"2" ,"3" ,"0" ,"3" ,"3" ,"1" ,"2" ,"3" ,"3" ,"3" ,"3" ,"3" ,"1" ,"1" ,"..
,"9" ,"5" ,"2" ,"3" ,"7" ,"7" ,"6" ,"7" ,"2" ,"0" ,"3" ,"5" ,"0" ,"5" ,"2" ,"..

The each-right adverb modifies the join function, so:

q)"exchange",/:n 0
"exchange3"
"exchange2"
"exchange3"
..

and we can modify the derived function ,/: with each-both ' to prefix the strings in the first vector with "exchange" and those in the second with "inst".

q)("exchange";"inst"),/:'n
"exchange3" "exchange2" "exchange3" "exchange0" "exchange3" "exchange3" "exch..
"inst9"     "inst5"     "inst2"     "inst3"     "inst7"     "inst7"     "inst..

Now we need only flip the pair of vectors into a vector of pairs

q)flip("exchange";"inst"),/:'n
"exchange3" "inst9"
"exchange2" "inst5"
"exchange3" "inst2"
"exchange0" "inst3"
"exchange3" "inst7"
..

and modify sv with each-right to join every pair:

q)"."sv/:flip("exchange";"inst"),/:'n
"exchange3.inst9"
"exchange2.inst5"
"exchange3.inst2"
"exchange0.inst3"
"exchange3.inst7"
..
SJT
  • 1,067
  • 5
  • 10