Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Hi I want to create A YML file and load them as instance variables instead. How can I do that.
yml_File.yml File ::
default:
browserversion: 43
dev:
browser_02: iexplore
qa_01:
browser_default: chrome
qa_02:
browser_default: safari
Check the below Code::
p yml_File = YAML.load_file(File.dirname(__FILE__).gsub('/', '\\') + '\\Profiles.yml')
yml_File.each_key {|key_Value|
va = yml_File[key_Value].to_s
var_name = "##{key_Value}" # the '#' is required
self.instance_variable_set(var_name, va)
p "Name of Instance variable '#{key_Value}' is :: " + var_name.to_s + ' - And Key value is : ' + eval("##{key_Value}")
}
p #dev
p #qa_01
p #qa_02
Note - Ruby 1.9+ atleast
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
Ok, so I am trying to convert a list of CIDR formatted ip data into netranges
192.168.1.0/24 I need that, converted to 192.168.1.0-255
In fact, a whole list of addresses. Like this.
101.192.0.0/14
101.202.0.0/16
101.203.128.0/19
101.248.0.0/15
101.252.0.0/15
103.16.76.0/24
103.194.8.0/22
103.197.8.0/22
103.205.84.0/22
103.207.160.0/22
103.210.12.0/22
103.215.80.0/22
103.229.36.0/22
103.229.40.0/22
103.230.144.0/22
103.232.136.0/22
103.232.172.0/22
103.236.32.0/22
Is there any tools readily available that do this? I did some searching before posting this, but nobody else seems to be going for the specific output that I am trying to get here.
Just tried without external commands or libraries.
perl -pe '
m#^(\d+)\.(\d+)\.(\d+)\.(\d+)/(\d+)#;
$_ = "$1.$2.$3.$4-" . join(".", map(hex, sprintf("%X", ($1 << 24) + ($2 << 16) + ($3 << 8) + $4 | (1 << (32 - $5)) - 1) =~ /.{2}/g)) . "\n";
' iplist.txt
I provide the sample script using ipaddress.(Thank you for the instructions, #Jonathon Reinhart )
The prerequisite task is just installing ipaddress module.
# pip install ipaddress
script code: the cidr.list file is including above cidr list.
#!/usr/bin/env python
import ipaddress
file_path='/tmp/cidr.list'
with open(file_path,'r') as cidr_list:
for cidr in cidr_list:
tmp = ipaddress.ip_network(unicode(cidr[:-1]))
print "%s-%s" % (tmp[0],tmp[-1])
output:
101.192.0.0-101.195.255.255
101.202.0.0-101.202.255.255
101.203.128.0-101.203.159.255
101.248.0.0-101.249.255.255
101.252.0.0-101.253.255.255
103.16.76.0-103.16.76.255
103.194.8.0-103.194.11.255
103.197.8.0-103.197.11.255
103.205.84.0-103.205.87.255
103.207.160.0-103.207.163.255
103.210.12.0-103.210.15.255
103.215.80.0-103.215.83.255
103.229.36.0-103.229.39.255
103.229.40.0-103.229.43.255
103.230.144.0-103.230.147.255
103.232.136.0-103.232.139.255
103.232.172.0-103.232.175.255
103.236.32.0-103.236.35.255
I hope this will help you.
Update (thanks #TheAdminsHereAreFags for pointing out sipcalc)
If you have the sipcalc package installed
sipcalc - < iplist.txt | awk '/Network range/{print $4 $5 $6}'
Output
101.192.0.0-101.195.255.255
101.202.0.0-101.202.255.255
101.203.128.0-101.203.159.255
101.248.0.0-101.249.255.255
101.252.0.0-101.253.255.255
103.16.76.0-103.16.76.255
103.194.8.0-103.194.11.255
103.197.8.0-103.197.11.255
103.205.84.0-103.205.87.255
103.207.160.0-103.207.163.255
103.210.12.0-103.210.15.255
103.215.80.0-103.215.83.255
103.229.36.0-103.229.39.255
103.229.40.0-103.229.43.255
103.230.144.0-103.230.147.255
103.232.136.0-103.232.139.255
103.232.172.0-103.232.175.255
103.236.32.0-103.236.35.255
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
{"headends"=>
[{"headend"=>
{"id"=>341766992,
"headend_name"=>"Comcast Burlingame Digital",
"dma_code"=>"807",
"dma_rank"=>6,
"system_name"=>"Comcast",
"headend_city"=>"Burlingame",
"headend_state"=>"CA",
"headend_time_zone"=>"PT",
"dma_name"=>"SAN FRANCISCO-OAK-SAN JOSE",
"channel_device"=>"X",
"country"=>"",
"service_type"=>"CA"},
"mso"=>{"id"=>341775346, "mso_name"=>"Comcast Cable Communications"},
"postal_code"=>"94010",
"device_id"=>"5b9a5042"}],
"services"=>
["amazon",
"directv",
"hbogo",
"hulu",
"itunes",
"itunes",
"netflixusa",
"showtime",
"vudu",
"youtube"],
"postal_code"=>nil,
"apps"=>
["cf528ea9",
"ea0f81d1",
"2ba2dc0e",
"50107ad3",
"3c103fa4",
"692bea67",
"557e96d5",
"b2db5e2a",
"0247ee5a",
"f0ad77dc",
"b24c00b1"]}
This is my hash, how can i extract values like "id"=>341766992, "postal_code"=>"94010"
For things that are hashes, e.g. {"foo"=>"bar", "baz"=>"blah"}, index into them with the key, e.g. myhash["foo"] # "baz".
For things that are arrays, e.g. ["hello", "world"], use their 0-based numeric indices, e.g. myarray[1] # "world".
Put those things together to dig through your structure, which I pretty-printed in an edit to your question:
data = {"headends"=>[{"headend"=>{"id"=>341766992, "headend_name"=>"Comcast Burlingame Digital", "dma_code"=>"807", "dma_rank"=>6, "system_name"=>"Comcast", "headend_city"=>"Burlingame", "headend_state"=>"CA", "headend_time_zone"=>"PT", "dma_name"=>"SAN FRANCISCO-OAK-SAN JOSE", "channel_device"=>"X", "country"=>"", "service_type"=>"CA"}, "mso"=>{"id"=>341775346, "mso_name"=>"Comcast Cable Communications"}, "postal_code"=>"94010", "device_id"=>"5b9a5042"}], "services"=>["amazon", "directv", "hbogo", "hulu", "itunes", "itunes", "netflixusa", "showtime", "vudu", "youtube"], "postal_code"=>nil, "apps"=>["cf528ea9", "ea0f81d1", "2ba2dc0e", "50107ad3", "3c103fa4", "692bea67", "557e96d5", "b2db5e2a", "0247ee5a", "f0ad77dc", "b24c00b1"]}
puts data["headends"][0]["headend"]["id"]
puts data["headends"][0]["postal_code"]
# Output:
# 341766992
# 94010
Prior to Ruby 2.3:
input['headends'].map do |e|
[
e['postal_code'],
*e['headend'].values_at(*%w|id|),
*e['mso'].values_at(*%w|id|),
]
end
2.3+
input['headends'].map do |e|
[%w|postal_code|, %w|headend id|, %w|mso id|].map do |key|
e.dig(*key)
end
end
Your question has been answered but I'm posting this to better show the format of the hash and also to point out that the example given could be drastically reduced in size and still make the same point.
h = { "headends"=>
[
{ "headend"=> {
"id" =>341766992,
"channel_device"=>"X",
"service_type" =>"CA"
},
"mso"=> {
"id" =>341775346,
"mso_name"=>"Comcast Cable Communications"
},
"postal_code"=>"94010",
"device_id" =>"5b9a5042"
}
]
}
h["headends"][0]["headend"]["id"] #=> 341766992
h["headends"][0]["postal_code"] #=> "94010"
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Unable to Write Inline Aggregate function in Matlab.
X1, X2 are array variables. And mb and nb are size of BUS DATA.
V is the voltage function, delta is the angle.
% objf=inline('sum(V(mb)^2+V(nb)^2-2*V(mb)*V(nb)*cos(delta(mb)-delta(nb)))','mb','nb');
% old code running
objf=inline('4*x1^2-2.1*x1^4+(x1^6)/3+x1*x2-4*x2^2+4*x2^4','x1','x2');**
*Error using inlineeval (line 15)
Error in inline expression ==> sum(V(mb).^2+V(nb).^2-2.*V(mb).*V(nb).cos(delta(mb)-delta(nb)))
Undefined function 'V' for input arguments of type 'double'.
Error in inline/subsref (line 24)
INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);
Error in deeee (line 48)
fx=objf(x(:,1),x(:,2));
where variable aer defined as below..
busdata = bus; % ARRAY OF INPUTs
j=sqrt(-1);
P=[];Q=[];
nb=busdata(:,1);
kb=busdata(:,2);Vm=busdata(:,3);deltad=busdata(:, 4);Pd=0.8*busdata(:,5)/basemva;Qd=.8*busdata(:,6)/basemva;
Pg=busdata(:,7)/basemva;Qg=busdata(:,8)/basemva;Bsh=busdata(:,11);Qmin=busdata(:,9)/basemva;Qmax=busdata(:,10)/basemva;
G=real(Ybus);B=imag(Ybus);slb=find(kb==1);pv=find(kb==2);pq=find(kb==0);pvq=find(kb~=1);npv=length(pv);
npq=length(pq);npvq=length(pvq);nbus=max(nb);
delta(nb) = pi/180*deltad(nb);
V(nb) = Vm(nb).*(cos(delta(nb))+j*sin(delta(nb)))';
P(nb)=(Pg(nb)-Pd(nb));
flag=0;
What you're locking for is anonymous functions
objf = #(mb,nb)sum(V(mb)^2+V(nb)^2-2*V(mb)*V(nb)*cos(delta(mb)-delta(nb)))
objf =
#(mb,nb)sum(V(mb)^2+V(nb)^2-2*V(mb)*V(nb)*cos(delta(mb)-delta(nb)))
objf(1,2)
There you go (as far as all other variables and functions of this anonymous function are defined).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to convert this:
[{"day"=>"2014-02-01", "users"=>1234},
{"day"=>"2014-02-02", "users"=>2234},
{"day"=>"2014-02-03", "users"=>3234},
{"day"=>"2014-02-04", "users"=>4234}]
into this:
[{:x=>1, y:=>1234},
{:x=>2, y:=>2234},
{:x=>3, y:=>3234},
{:x=>4, y:=>4234}]
a = [{"day"=>"2014-02-01", "users"=>1234}, {"day"=>"2014-02-02", "users"=>2234}, {"day"=>"2014-02-03", "users"=>3234}, {"day"=>"2014-02-04", "users"=>4234}]
a.map.with_index(1) { |h,i| { :x => i, :y => h['users'] } }
# => [{:x=>1, :y=>1234}, {:x=>2, :y=>2234}, {:x=>3, :y=>3234}, {:x=>4, :y=>4234}]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i want to load my image call caltrain, there is 30 img.
i used code
for i = 0:30
imgINumber = i;
imgPNumber = i+2;
if imgINumber < 10
imgIFile = sprintf('C:\sequence01_caltrain_gray\caltrain/gray/%s00%d.ras',imageName, imageName, imgINumber);
elseif imgINumber < 100
imgIFile = sprintf('C:\sequence01_caltrain_gray\caltrain/gray/%s0%d.ras',imageName, imageName, imgINumber);
end
if imgPNumber < 10
imgPFile = sprintf('C:\sequence01_caltrain_gray\caltrain\gray/%s00%d.ras',imageName, imageName, imgPNumber);
elseif imgPNumber < 100
imgPFile = sprintf('C:\sequence01_caltrain_gray\caltrain\gray/%s0%d.ras',imageName, imageName, imgPNumber);
end
imgI = double(imread(imgIFile));
imgP = double(imread(imgPFile));
imgI = imgI(:,1:352);
imgP = imgP(:,1:352);
but error:
Error using ==> imread
Can't open file "C:" for reading;
you may not have read permission.
i need solution for this
thanks
Either double your backslashes or replace all the backslashes with slashes in your sprintf calls.