Get name of an hdf5 group using pytables - hdf5

import tables
f = tables.open_file('my_file.hdf5', mode='w')
f.create_group(f.root, name='videos')
I have another script that adds data to the 'videos' group. This script checks that the 'videos' group already exists; if 'videos' does not yet exist, the script will create a 'videos' group.
How can I check if a group already exists? I tried using f.walk_groups() and f.root._v_groups, but these don't seem like the best solution. And how can I get a list containing the names (as strings) of all the groups in f.root?

All you need to do is check for the existence of the group/dataset path name in the root group. This will return True/False: '/videos' in f.root
So you can create a if block like this:
if '/videos' in f.root:
do something
Here's a short code segment that creates a group + 2 array datasets, then prints the logical tests for the group and 2 datasets. Note that arr3 does not exist.
import tables as tb
import numpy as np
with tb.File('SO_65327481.hdf5', mode='w') as h5f:
h5f.create_group('/', name='videos')
arr = np.random.random((10,10))
h5f.create_array('/videos',name='arr1', obj=arr)
arr = np.random.random((10,10))
h5f.create_array('/videos','arr2',obj=arr)
print ( 'videos exists:', '/videos' in h5f.root)
print ( 'arr2 exists:', '/videos/arr2' in h5f.root)
print ( 'arr3 exists:', '/videos/arr3' in h5f.root)
Output will be:
videos exists: True
arr2 exists: True
arr3 exists: False

Related

How can I get the line of a table while I got its address

local a = {'1'}
b = {'2'}
print('--------a:', a) --------a: table: 002411A0
print('--------b:', b) --------b: table: 005BC470
how can I get like: a.lua:1 in table a
or a.lua:2 in table b
while I know the table address (002411A0)
my lua environment is lua5.1, I don't know if I need to read the source or compiled of lua5.1?
If you are prepared to declare your tables with a helper function, called, say logger, you can achieve your goal.
The idea is to record the line in a virtual table field __line. In the example below I do it using __index metamethod, but you can simply add a field to the created table.
The line number is obtained by debug.getinfo (2).currentline. The choice of 2 is determined by the call stack depth in my example.
local function logged (t)
local line = debug.getinfo (2).currentline
return setmetatable (t, {
__index = function (_, key)
if key == '__line' then
return line
end
end
})
end
local a = logged {'1'}
print (a.__line) -- 12
b = logged {'2'}
print (b.__line) -- 14
There is no way to get a line where the table is defined by using its address (as the line in the source code and the address in memory have no relationship). What you can do is to parse the source code of your script and find where the definition of the table is, but I'm not sure what use it's going to have for you. Maybe you can describe what you are trying to do?
If you indeed want to find where the table is defined, you can use something like metalua that builds abstract syntax tree (AST) of your code fragment that you can then traverse to find where a particular table is defined.
Another option is to parse the output of luac compiler, which will allow you to find what line the NEWTABLE command for a particular table is on.

How do I not add items to a table that exist in another?

I'm creating a gaming server that allows Lua scripts. Basically, the script gets the server date and selects a text file based on that date. Each text file contains a list of names. The point of the script is to rename players a "fun" holiday name.
This is my initial code to populate a table and assign a name:
-- Get Names from selected Holiday file
local holFile = io.open(filePath .. holiday .. ".txt", "r");
local holidayNames = {}
for line in holFile:lines() do
table.insert (holidayNames, line);
end
-- Set Name to a random item in the Holiday Names table
randomItem = math.random(0, #holidayNames - 1)
Name = (holidayNames[randomItem])
I also added this part BEFORE the above code just to have a table populated with current names:
-- Get Current Players List
local currPlayers = io.open(filePath "players.txt", "r");
local currentPlayers = {}
for line in currPlayers:lines() do
table.insert (currentPlayers, line);
end
So basically, as I'm attempting to add items to holidayNames, I want to see if they exist in currentPlayers, first.
Since currentPlayers is already defined, you'll have to scan it per each line in holiday names to look for a match. You can do this using pairs:
for line in holFile:lines() do
for __, name in pairs(currentPlayers) do
if name ~= line then
-- skip insertion if it's a match
table.insert(holidayNames, line)
end
end
end

How to aggregate data using apache beam api with multiple keys

I am new to google cloud data platform as well as to Apache beam api. I would like aggregate data based on multiple keys. In my requirement I will get a transaction feed having fields like customer id,customer name,transaction amount and transaction type. I would like to aggregate the data based on customer id & transaction type. Here is an example.
customer id,customer name,transction amount,transaction type
cust123,ravi,100,D
cust123,ravi,200,D
cust234,Srini,200,C
cust444,shaker,500,D
cust123,ravi,100,C
cust123,ravi,300,C
O/p should be
cust123,ravi,300,D
cust123,ravi,400,C
cust234,Srini,200,C
cust444,shaker,500,D
In google most of the examples are based on single key like group by single key. Can any please help me on how my PTransform look like in my requirement and how to produce aggregated data along with rest of the fields.
Regards,
Ravi.
Here is an easy way. I concatenated all the keys together to form a single key and then did the the sub and after than split the key to organize the output to a way you wanted. Please let me know if any question.
The code does not expect header in the CSV file. I just kept it short to show the main point you are asking.
import apache_beam as beam
import sys
class Split(beam.DoFn):
def process(self, element):
"""
Splits each row on commas and returns a tuple representing the row to process
"""
customer_id, customer_name, transction_amount, transaction_type = element.split(",")
return [
(customer_id +","+customer_name+","+transaction_type, float(transction_amount))
]
if __name__ == '__main__':
p = beam.Pipeline(argv=sys.argv)
input = 'aggregate.csv'
output_prefix = 'C:\\pythonVirtual\\Mycodes\\output'
(p
| 'ReadFile' >> beam.io.ReadFromText(input)
| 'parse' >> beam.ParDo(Split())
| 'sum' >> beam.CombinePerKey(sum)
| 'convertToString' >>beam.Map(lambda (combined_key, total_balance): '%s,%s,%s,%s' % (combined_key.split(",")[0], combined_key.split(",")[1],total_balance,combined_key.split(",")[2]))
| 'write' >> beam.io.WriteToText(output_prefix)
)
p.run().wait_until_finish()
it will produce output as below:
cust234,Srini,200.0,C
cust444,shaker,500.0,D
cust123,ravi,300.0,D
cust123,ravi,400.0,C

SPSS merge datasets with add variables only links 1 case

I have the following syntax to merge two datasets. I expect that the resulting dataset (test1) contains 5 cases with 4 of them (2 to 5) a value in variable set2.
The result I am getting is dataset test1 with 5 cases but only 1 of them (case with id 5) has a value in variable set2.
Do I need to contact my ICT department, or am I misunderstanding something about merging data in SPSS. I am used to working with SAS, R and SQL, but need to help someone with a data merging within SPSS
INPUT PROGRAM.
LOOP id=1 to 5.
END CASE.
END LOOP.
END FILE.
END INPUT PROGRAM.
COMPUTE set1 = RV.NORMAL(1,1).
EXECUTE.
DATASET NAME test1.
INPUT PROGRAM.
LOOP id=2 to 5.
END CASE.
END LOOP.
END FILE.
END INPUT PROGRAM.
COMPUTE set2 = RV.NORMAL(1,1).
EXECUTE.
DATASET NAME test2.
DATASET ACTIVATE test1.
STAR JOIN
/SELECT t0.set1, t1.set2
/FROM * AS t0
/JOIN 'test2' AS t1
ON t0.id=t1.id
/OUTFILE FILE=*.
results in:
id set1 set2
1,00 1,74
2,00 1,58
3,00 1,01
4,00 ,12
5,00 2,52 ,79
SPSS version 21
When I run the syntax you provide I get the desired results (and not what you indicate):
If it continues to fail (after contacting SPSS support), try using MATCH FILES:
DATASET ACTIVATE test1.
SORT CASES BY ID.
DATASET ACTIVATE test2.
SORT CASES BY ID.
MATCH FILES FILE=test1 /FILE=test2 /BY ID.
DATASET NAME Restult.

Trying to match values from one raw file with another raw file values in Lua

First of all: I'm an inexperienced coder and just started reading PiL. I only know a thing or two but I'm fast learning and understanding. This method is really unnecessary but I sort of want to give myself a hard time in order to learn more.
Okay so for testing and for getting to know the language more, I'm trying to grab two different values from two different files and storing them in tables
local gamemap = file.Read("addons/easymap/data/maplist.txt", "GAME")
local mapname = string.Explode( ",", gamemap )
local mapid = file.Read("addons/easymap/data/mapid.txt", "GAME")
local id = string.Explode( ",", mapid )
I'm grabbing two values which in the end are mapname and id
Once I have them, I know that using
for k, v in pairs(mapname)
It will give specific values to the data taken from the file, or at least assign them.
But what I need to do with the both tables is that if there is certain map in the server, check for the value in the table unless the map name is nil and then once having the name, grab the value of that map and match it with the id of the other file.
For example, I have in the maplist.txt file gm_construct and it is the first entry [1] and its corresponding id in mapid.txt lets say it is 54321 and it is also the first entry [1].
But now I must check the server's current map with game.GetMap function, I have that solved and all, I grab the current map, match it with the mapname table and then check for its corresponding value in the id table, which would be gm_construct = 1.
For example it would be something like
local mapdl = game.GetMap()
local match = mapname[mapdl]
if( match != nil )then --supposing the match isn't nil and it is in the table
--grab its table value, lets say it is 1 and match it with the one in the id table
It is a more complex version of this http://pastebin.com/3652J8Pv
I know it is unnecessary but doing this script will give me more options to expand the script further.
TL;DR: I need to find a function that lets me match two values coming from different tables and files, but in the end they are in the same order ([1] = [1]) in both files. Or a way to fetch a full table from another file. I don't know if a table can be loaded globally and then grabbed by another file to use it in that file.
I'm sorry if I'm asking too much, but where I live, if you want to learn to program, you have to do it on your own, no schools have classes or anything similar, at least not until University, and I'm far away from even finishing High School.
Edit: this is intended to be used on Garry's mod. The string.Explode is explained here: http://wiki.garrysmod.com/page/string/Explode
It basically separates phrases by a designated character, in this case, a comma.
Okay. If I understand correctly... You have 2 Files with data.
One with Map Names
gm_construct,
gm_flatgrass,
de_dust2,
ttt_waterworld
And One with IDs, Numbers, Whataver (related to the entries at the same position in the Map Names File
1258,
8592,
1354,
2589
And now you want to find the ID of the current Map, right?
Here is your Function
local function GetCurrentMapID()
-- Get the current map
local cur_map = game.GetMap()
-- Read the Files and Split them
local mapListRaw = file.Read("addons/easymap/data/maplist.txt", "GAME")
local mapList= string.Explode(",", mapListRaw)
local mapIDsRaw = file.Read("addons/easymap/data/mapid.txt", "GAME")
local mapIDs = string.Explode(",", mapIDsRaw)
-- Iterate over the whole map list
for k, v in pairs(mapList) do
-- Until you find the current map
if (v == cur_map) then
-- then return the value from mapIDs which is located at the same key (k)
return mapIDs[k]
end
end
-- Throw a non-breaking error if the current map is not in the Maplist
ErrorNoHalt( "Current map is not registered in the Maplist!\n" )
end
Code could have errors 'cause I couldn't test it. Pls Comment with error if so.
Source: My Experience and the GMod Wiki

Resources