Suitable Data Type - data-structures

i got a list of user name and user id. i want to group the user name by initial. the key will be the user name, so i can get user id from the user name and check if user exist or not.
what is the best data type to use for this? i'm thinking of hash, but any other recommendation? can you tell me why should i choose that data type?
thanks!

I was just playing with that. So here is what I got:
> set alias:tamer 1000
"OK"
> set alias:tansel 1001
"OK"
> hmset uid:1000 alias tamer age 45
"OK"
> hmset uid:1001 alias tansel age 39
"OK"
> hset uid:1000 pass x1x2x3
true
============================
> get alias:tamer
"1000"
> hget uid:1000 alias
"tamer"
hgetall uid:1000
{"alias":"tamer","age":"45","pass":"x1x2x3"}
> hgetall uid:1001
{"alias":"tansel","age":"39"}
> hvals uid:1000
["tamer","45","x1x2x3"]
> hkeys uid:1000
["alias","age","pass"]
So if someone is trying to log in they will give you alias and pass
You would then check if the alias exists, like so:
> get alias:tamer
"1000"
We are checking if uid:tamer is in the database. The answer is yes and the uid # is 1000
Lets get the rest of the uid info:
> hgetall uid:1000
{"alias":"tamer","age":"45","pass":"x1x2x3"}
OR just the password:
> hget uid:1000 pass
"x1x2x3"
Does this answer your question?

Related

Get data from tables with same id column

I have 3 tables:
features:
id | name
branch_features:
id | branch_id | feature_id
permissions:
id | name | feature_id
I'm trying to get the features that only exist in branch_features with permissions. So the end result should be something like that:
Feature 1
Permission 1
Permission 2
Feature 4
Permission 7
Permission 8
I tried hasManyThrough and hasMany. But no success:
return $this->hasManyThrough(Permission::class, BranchFeature::class, 'feature_id', 'feature_id');
Could it be done without joining the 3 tables like that?
return $this->join('branch_features AS bf', 'bf.feature_id', 'features.id')->join('permissions AS p', 'p.feature_id', 'features.id')
Because I want to sort each permission beneath its feature like that:
Feature X
Permission A
Permission B

Change or personalize OID

Finally managed to have my RPI computer module to work with SNMP.
I have a script running that gives me one of my parameters and if I use query using SNMP I get the info back.
pi#raspberrypi:~ $ snmpwalk -v2c -c public localhost NET-SNMP-EXTEND-MIB::nsExtendObjects | grep snmp_status
NET-SNMP-EXTEND-MIB::nsExtendCommand."snmp_status" = STRING: /home/pi/BDC/snmp_status.py
NET-SNMP-EXTEND-MIB::nsExtendArgs."snmp_status" = STRING:
NET-SNMP-EXTEND-MIB::nsExtendInput."snmp_status" = STRING:
NET-SNMP-EXTEND-MIB::nsExtendCacheTime."snmp_status" = INTEGER: 5
NET-SNMP-EXTEND-MIB::nsExtendExecType."snmp_status" = INTEGER: exec(1)
NET-SNMP-EXTEND-MIB::nsExtendRunType."snmp_status" = INTEGER: run-on-read(1)
NET-SNMP-EXTEND-MIB::nsExtendStorage."snmp_status" = INTEGER: permanent(4)
NET-SNMP-EXTEND-MIB::nsExtendStatus."snmp_status" = INTEGER: active(1)
NET-SNMP-EXTEND-MIB::nsExtendOutput1Line."snmp_status" = STRING: 0
NET-SNMP-EXTEND-MIB::nsExtendOutputFull."snmp_status" = STRING: 0
NET-SNMP-EXTEND-MIB::nsExtendOutNumLines."snmp_status" = INTEGER: 1
NET-SNMP-EXTEND-MIB::nsExtendResult."snmp_status" = INTEGER: 0
NET-SNMP-EXTEND-MIB::nsExtendOutLine."snmp_status".1 = STRING: 0
If my unit is in alarm replies with
NET-SNMP-EXTEND-MIB::nsExtendOutLine."snmp_status".1 = STRING: 1
if not in alarm replies with
NET-SNMP-EXTEND-MIB::nsExtendOutLine."snmp_status".1 = STRING: 0
This status is stored in a file and it's parsed to the SNMP using a python script.
Now... next question.
The SNMP server gives me the following OID
.1.3.6.1.4.1.8072.1.3.2.3.1.2.11.115.110.109.112.95.115.116.97.116.117.115
and for each parameter it gives me one very different IOD.
How can I change this for something more easy... like the ones we see on MIB files?
If you are doing it in the command line, use
snmptranslate -m NET-SNMP-EXTEND-MIB .1.3.6.1.4.1.8072.1.3.2.3.1.2.11.115.110.109.112.95.115.116.97.116.117.115
To do it purely programmatically (i.e. without parsing command line output), you will need a way to parse the MIB files. I think such tools probably exist in Python, but I've never used them myself.
More often, I hard-code constants for the OIDs that I'm interested in, and manually inspect the MIB to know how to decode the index for each object. The OID you gave is an instance of NET-SNMP-EXTEND-MIB::nsExtendOutputFull, which belongs to nsExtendOutput1Entry. Normally the *Entry types will have an INDEX field telling you which field is used as the index of that table. In this case, it has an AUGMENTS field instead, which points you to nsExtendConfigEntry. The INDEX fornsExtendConfigEntry is nsExtendToken, which has a type of DisplayString (basically an OCTET STRING that is limited to human-readable characters).
Here's an example of how I would do this in Python -- you'll need pip install snmp:
from snmp.types import OID, OctetString
nsExtendOutputFull = OID.parse(".1.3.6.1.4.1.8072.1.3.2.3.1.2")
oid = OID.parse(".1.3.6.1.4.1.8072.1.3.2.3.1.2.11.115.110.109.112.95.115.116.97.116.117.115")
nsExtendToken = oid.extractIndex(nsExtendOutputFull, OctetString)
print(f"Index = {nsExtendToken}")
Here's the output:
Index = OctetString(b'snmp_status')

Ruby is there a way to stop the user from calling a function/procedure through case before they have accessed a different function/procedure?

I have a text file that I want to open first for reading or writing, but want the user to manually enter the text_file name (which opens the file for reading) first like so:
def read_in_albums
puts "Enter file name: "
begin
file_name = gets().chomp
if (file_name == "albums.txt")
puts "File is open"
a_file = File.new("#{file_name}", "r")
puts a_file.gets
finished = true
else
puts "Please re-enter file name: "
end
end until finished
end
From this unfinished code below, selecting 1 would go to the above procedure. I want the user to select 1 first, and if they choose 2 without having gone through read_in_albums they just get some sort of message like "no file selected and sent back to menu screen.
def main()
finished = false
begin
puts("Main Menu:")
puts("1- Read in Album")
puts("2- Display Album Info")
puts("3- Play Album")
puts("4- Update Album")
puts("5- Exit")
choice = read_integer_in_range("Please enter your choice:", 1, 5)
case choice
when 1
read_in_albums
when 2
display_album_info
when 5
finished = true
end
end until finished
end
main()
The only thing I can think of is something like
when 2
if(read_in_albums == true)
display_album_info
and have it return true from read_in_albums.
which I don't want to do as it just goes through read_in_albums again, when I only want it to do that if the user pressed 1.
All of your application's functionality depends on whether the album data has been read. You are no doubt storing this data as an object in memory referenced by some variable.
$album_data = File.read 'album.txt'
You can test whether this data is present in order to determine whether the file data has been read:
if $album_data.nil?
# ask user for album file
else
# show album user interface
end
There is no need for a separate flag. The mere presence of the data in memory serves as a flag already.
You could either set a flag when option 1 was selcted
has_been_read = false
...
when 1
read_in_albums
has_been_read = true
when 2
if has_been_read
display_album_info
else
puts "Select Option 1 first"
end
Or just test if your file name is a valid string.

In Ruby how do I search a string line by line and only show lines that begin with 1

What i am wanting to do is to ssh into a ubiquiti device, run brctl showmacs br0 and only retrieve the mac addresses on the local port (1) for instance:
1 d4:ca:6d:ec:aa:fe no 0.05
would be printed/put/written-to-file because it begins with a 1 while:
2 4c:5e:0c:d5:ba:95 no 38.62
will not.
Strings respond to []; so you could take your collection #collection and :select where x[0] == '1'.
only_ones = #collection.select{|x| x[0] == '1' }
You can use SSHKit to run a remote command:
on 'ubiquiti.yourdomain.com' do
output = capture(:brctl, 'showmacs br0')
puts output.lines.select{|line| line.start_with? "1"}
end

Ruby command line option that lists operations to perform

Is it possible for me to do something like this:
>ruby some_file.rb
>Your options are:
> 1. delete file blah.txt
> 2. delete file blah2.txt
> 3. delete file blah3.txt
> x to exit
> 1
> blah.txt was deleted
> 1. delete file blah.txt
> 2. delete file blah2.txt
> 3. delete file blah3.txt
> x to exit
> x
>
And this would read it's configuration from a file which would have all the files to delete.
I want this to be in a loop, so it keeps asking what to do unless you press 'x'.
How could you do this in Ruby?
You can read user input from standard input using Kernel#gets. This should hopefully point you in the right direction.
You need to create the configuration file your talking about that has all of the files you
need to delete.
Read the configuration file
Create a menu based on the configuration file
Read in user input and perform operations.
You can start from here:
def menu(choice)
case choice
when 1
# do_something
when 2
# ...
end
end
while ((a = gets) != "x\n")
menu(a)
end
as #Hunter McMillen stated you can use some configuration file to create the case-when programmatically.
if ARGV.size > 0
if FileTest.exists? ARGV[0]
files=[]
IO.readlines(ARGV[0]).each{|l| files.push(l.chop) if FileTest.exists? l.chop}
while files.size > 0
files.each_with_index{|f,i| puts "#{i+1}. #{f}"}
puts "x to exit"
opt = $stdin.gets.chop
break if opt == 'x'
opt = opt.to_i
puts "#{files.delete_at(opt-1)} was deleted" if opt > 0 && opt <= files.size && File.delete(files[opt-1]) > 0
end
end
end

Resources