First sorry for my english...
I need your help to fix a difficult. I have a python script to check some parameters, with this parameters I check some conditions but I would like to add the possibility for the user to create an another check from a string...
To give you an exemple, I will save some coditions in string than I will split to have the check name, limit value,delay,condition (<,>,=...). And from this list (string splited) I woud like to create an "If 'value' 'condition' 'limit value' " but I don't find how to write this IF from my list.
parameter="O2_alert,O2,0.015,<"
para1=parameter.split(',') # ==> para1 =["O2_alert","O2","0.015","<"]
alert_name=para1[0]
value_name=para1[1] # it's a name of a variable in my script not a value
limit_value=para1[2]
condition=para1[3]
# And I woul like to have
if value_name condition limit_value :
Thanks for your help.
You can use like this,
parameter="02_alert,2,0.015,<"
para1=parameter.split(',') # ==> para1 =["02_alert","02","0.015","<"]
alert_name=para1[0]
value_name=para1[1] # it's a name of a variable in my script no a value
limit_value=para1[2]
condition=para1[3]
if eval(f"{value_name} {condition} {limit_value}"):
print('abcd')
It works for you.
Related
I'm trying to develop a maco in python. I need a method that changes the URI given in the input variables.
For instance, in www.(variable).com I need to change the URL www.1.com to www.2.com
You can use formatted strings like this:
variable = 1
url = f"www.{variable}.com"
You could store your list of names in a list and then interpolate the variable into the f-string like:
names = ['google', 'amazon', 'microsoft']
for name in names:
print(f"www.{name}.com")
OUTPUT
www.google.com
www.amazon.com
www.microsoft.com
I want to find a specific string within a string.
For example, let's say I have the string
string = "username:quantopia;password:blabla
How can I then find quantopia?
I am using python 3.
Update: I am sorry I did not mention what I try before..
string.split('username:',1)[1].split(';',1)[0]
But this look very bad and not efficient, I was hoping for something better.
Just use regex as such:
import re
username = re.search("username:(.*);password", "username:quantopia;password:blabla").group(1)
print("username:", username)
This will output quantopia.
In this expression "username:(.*);password" you are saying "give me everything from username: to ;password" So this is why you're getting quantopia. This might as well be ":(.*);" as it will output the same thing in this case.
The simple solution is:
string = "username:quantopia;password:blabla"
username = "username"
if username in string:
# do work.
You might be better to just use split to create a dictionary so you dont need to use multiple regex to extract different parts of data sets. The below will split stirng into key value pairs then split key value pairs then pass the list of lists to dict to create a dictionary.
string = "username:quantopia;password:blabla"
data = dict([pairs.split(':') for pairs in string.split(';')])
print(f'username is "{data["username"]}" and password is "{data["password"]}"')
OUTPUT
username is "quantopia" and password is "blabla"
I am fairly new to Puppet and Ruby. Most likely this question has been asked before but I am not able to find any relevant information.
In my puppet code I will have a string variable retrieved from the fact hostname.
$n="$facts['hostname'].ex-ample.com"
I am expecting to get the values like these
DEV-123456-02B.ex-ample.com,
SCC-123456-02A.ex-ample.com,
DEV-123456-03B.ex-ample.com,
SCC-999999-04A.ex-ample.com
I want to perform the following action. Change the string to lowercase and then replace the
-02, -03 or -04 to -01.
So my output would be like
dev-123456-01b.ex-ample.com,
scc-123456-01a.ex-ample.com,
dev-123456-01b.ex-ample.com,
scc-999999-01a.ex-ample.com
I figured I would need to use .downcase on $n to make everything lowercase. But I am not sure how to replace the digits. I was thinking of .gsub or split but not sure how. I would prefer to make this happen in a oneline code.
If you really want a one-liner, you could run this against each string:
str
.downcase
.split('-')
.map
.with_index { |substr, i| i == 2 ? substr.gsub(/0[0-9]/, '01') : substr }
.join('-')
Without knowing what format your input list is taking, I'm not sure how to advise on how to iterate through it, but maybe you have that covered already. Hope it helps.
Note that Puppet and Ruby are entirely different languages and the other answers are for Ruby and won't work in Puppet.
What you need is:
$h = downcase(regsubst($facts['hostname'], '..(.)$', '01\1'))
$n = "${h}.ex-ample.com"
notice($n)
Note:
The downcase and regsubst functions come from stdlib.
I do a regex search and replace using the regsubst function and replace ..(.)$ - 2 characters followed by another one that I capture at the end of the string and replace that with 01 and the captured string.
All of that is then downcased.
If the -01--04 part is always on the same string index you could use that to replace the content.
original = 'DEV-123456-02B.ex-ample.com'
# 11 -^
string = original.downcase # creates a new downcased string
string[11, 2] = '01' # replace from index 11, 2 characters
string #=> "dev-123456-01b.ex-ample.com"
I have created a structure containing a few different fields. The fields contain data from a number of different subjects/participants.
At the beginning of the script I prompt the user to enter the "Subject number" like so:
prompt='Enter the subject number in the format SUB_n: ';
SUB=input(prompt,'s');
Example SUB_34 for the 34th subject.
I want to then name my structure such that it contains this string... i.e. I want the name of my structure to be SUB_34, e.g. SUB_34.field1. But I don't know how to do this.
I know that you can assign strings to a specific field name for example for structure S if I want field1 to be called z then
S=struct;
field1='z';
S.(field1);
works but it does not work for the structure name.
Can anyone help?
Thanks
Rather than creating structures named SUB_34 I would strongly recommend just using an array of structures instead and having the user simply input the subject number.
number = input('Subject Number')
S(number) = data_struct
Then you could simply find it again using:
subject = S(number);
If you really insist on it, you could use the method proposed in the comment by #Sembei using eval to get the struct. You really should not do this though
S = eval([SUB, ';']);
Or to set the structure
eval([SUB, ' = mydata;']);
One (of many) reasons not to do this is that I could enter the following at your prompt:
>> prompt = 'Enter the subject number in the format SUB_n: ';
>> SUB = input(prompt, 's');
>> eval([SUB, ' = mydata;']);
And I enter:
clear all; SUB_34
This would have the unforeseen consequence that it would remove all of your data since eval evaluates the input string as a command. Using eval on user input assumes that the user is never going to ever write something malformed or malicious, accidentally or otherwise.
i want to get defect id from the url using groovy code (To build custom code in tasktop).
for example: I will have an dynamic url generated say www.xyz.com/abc/defect_123/ now I want to retrieve that letter that always starts from 17th position. and return the string
Please help..
Thanks in advance
Here are two possibilities. Please note that the "substring" option is very strict and will always start from the 16th position (what happens if the domain changes from www.xyz.com to www.xyzw.com?)
def str = 'www.xyz.com/abc/defect_123/';
def pieces = str.tokenize('/'); // prints defect_123
def from16 = str.substring(16); // prints defect_123/
println from16;
println pieces.last();
You should define this as dynamic url in UrlMappings.groovy file:
"www.xyz.com/abc/$defect_id" (controller: 'YourController', action: 'method_name')
and you can access the defect_id variable from YourController using params.defect_id