Got NoMethodError while parsing yaml - ruby

I have a YAML file like that:
---
name: dummy
version: 0.2.0
title: dummy
summary: dummy
Now I tried to get the version number:
config = YAML.load_file('Index.yml')
oldversion = config[0]['version']
Why do it get the following error:
NoMethodError: undefined method `[]' for nil:NilClass

Try with this:
config = YAML.load_file('Index.yml')
oldversion = config['version']

With config[0] you are indexing a sequence/array, and at the top level of your yaml file you have a mapping, not a sequence, so leave that out: oldversion = config['version'] or change your YAML file to:
---
- name: dummy
version: 0.2.0
title: dummy
summary: dummy
if you eventually want a list of such objects (with name, version, etc.) in your configuration.

Related

Google automl_v1beta1 error "the provided location ID is not valid"

I am trying to call trained model from google colab with example provided.
But there is an error.
Who knows is it beta error or I have not set somethoing properly?
Thanks in advance.
The code
from google.cloud import automl_v1beta1 as automl
automl_client = automl.AutoMlClient()
# Create client for prediction service.
prediction_client =
automl.PredictionServiceClient().from_service_account_json(
'XXXXX.json')
# Get the full path of the model.
model_full_id = automl_client.model_path(
project_id, compute_region, model_id
)
# Read the file content for prediction.
#with open(file_path, "rb") as content_file:
snippet = "fsfsf" #content_file.read()
# Set the payload by giving the content and type of the file.
payload = {"text_snippet": {"content": snippet, "mime_type": "text/plain"}}
# params is additional domain-specific parameters.
# currently there is no additional parameters supported.
params = {}
response = prediction_client.predict(model_full_id, payload, params)
print("Prediction results:")
for result in response.payload:
print("Predicted class name: {}".format(result.display_name))
print("Predicted class score: {}".format(result.classification.score))
The eror msg^
InvalidArgument: 400 List of found errors: 1.Field: name; Message: The provided location ID is not valid.
You have to use a region that supports AutoML beta. This works for me:
create_dataset("myproj-123456", "us-central1", "my_dataset_id", "en", "de")
I clone the repo "python-docs-samples" :
$ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
I navigate to the automl examples
$ cd /home/MY_USER/python-docs-samples/language/automl/
I set the environment variables for [1]:
GOOGLE_APPLICATION_CREDENTIALS
PROJECT_ID
REGION_NAME
I typed:
$ python automl_natural_language_dataset.py create_dataset automltest1 False
I got this message:
Dataset name: projects/198768927566/locations/us-central1/datasets/TCN7889001684301386365
Dataset id: TCN7889001684301386365
Dataset display name: automltest1
Text classification dataset metadata:
classification_type: MULTICLASS
Dataset example count: 0
Dataset create time:
seconds: 1569367227
nanos: 873147000
I set the environment variable for :
DATASET_ID
Please note that I got this for the step 5.
I typed:
python automl_natural_language_dataset.py import_data $DATASET_ID "gs://$PROJECT_ID-lcm/complaints_manual.csv"
I got this message:
Processing import...
Dataset imported.

Search for particular string in file and replace the value

I am new to ruby and was scratching my head for something that seems pretty trivial. I was trying to search for particular string in file and replace that value with the new value . Here is what I have:
file = open('file.txt') { |f| f.grep(/release/) }
The contents of the file file.txt is as follows:
develop: 0.0.0
release: 0.0.4
test: 0.0.0
I am getting the output of the above line in array as follows :
["release: 0.0.4\n"]
How do I change just the value of this line in above file to the new version, something like this :
develop: 0.0.0
release: 0.0.5
test: 0.0.0
Based on above suggestions from #tadman, I was able to get it working as follows:
require 'yaml'
file = YAML.load_file('file')
file["release"] = "#{new_version}"
file.open('file', 'w') { |f| YAML.dump(file, f) }

Change user-defined variable from fastlane

I have a user-defined variable in my Xcode project - MY_VARIABLE:
I linked MY_VARIABLE also in my .plist file:
And then I use it in my code:
NSString *myVariable = [[NSBundle mainBundle] objectForInfoDictionaryKey:#"MY_VARIABLE"];
In the fastfile I have my AppStore lane and, only in that case, I would like to change the value of MY_VARIABLE.
I'm currently using:
ENV["MY_VARIABLE"] = "appStoreValue"
but this doesn't work.
After a bit of research I found a solution to this.
I'm using xcargs in the gym action, like:
gym(
scheme: "MyScheme",
configuration: "Release",
use_legacy_build_api: 1,
xcargs: "MY_VARIABLE=appStoreValue"
)
Thx to https://stackoverflow.com/a/56179405/5790492 and https://nshipster.com/xcconfig/
I've created the xcconfig file, added it to project in Info tab. For fastlane added this plugin to work with xcconfig. And now it looks like:
def bumpMinorVersionNumber
currentVersion = get_xcconfig_value(path: 'fastlane/VersionsConfig.xcconfig',
name: 'FC_VERSION')
versionArray = currentVersion.split(".").map(&:to_i)
versionArray[2] = (versionArray[2] || 0) + 1
newVersion = versionArray.join(".")
update_xcconfig_value(path: 'fastlane/VersionsConfig.xcconfig',
name: 'FC_VERSION',
value: newVersion.to_s)
UI.important("Old version: #{currentVersion}. Version bumped to: #{newVersion}")
end
def bumpBuildNumber
currentBuildNumber = get_xcconfig_value(path: 'fastlane/VersionsConfig.xcconfig',
name: 'FC_BUILD')
newBuildNumber = currentBuildNumber.to_i + 1
update_xcconfig_value(path: 'fastlane/VersionsConfig.xcconfig',
name: 'FC_BUILD',
value: newBuildNumber.to_s)
UI.important("Old build number: #{currentBuildNumber}. Build number bumped to: #{newBuildNumber}")
end

Puppet -> undefined method 'to_sym' for []:Array

this is my 10th day using puppet so sorry if there is not enough information.
I installed module 'statsd' from: https://forge.puppet.com/jdowning/statsd on my puppet-master and used it like in readme file.
class { 'statsd':
backends => ['./backends/graphite'],
graphiteHost => "graphite-relay.dev.sizmdx.com",
graphite_globalPrefix => 'statsite',
graphite_legacyNamespace => false,
stackdriver_sendTimerPercentiles => false,
configfile => '/etc/statsd/localConfig.js'
}
I keep getting this error:
Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Error from DataBinding 'hiera' while looking up 'statsd::librato_skipInternalMetrics': undefined method `to_sym' for []:Array on node beadmin-dev0-lior-cm-3f4gxw0f.sizmek
Warning: Not using cache on failed catalog
Error: Could not retrieve catalog; skipping run
We do have 'Heira' in the puppet project (altough I'm not familiar too much with that)
here the Heira.yaml:
:backends:
- yaml
:hierarchy:
- client/%{clientcert}
- roles/%{role}
- environment/%{env}
- global
:yaml:
:datadir: /opt/puppet/hiera
I google'd the error without any findings, any suggestions ?
if need more information please let me know.
thanks in advance

Ruby: add new fields to YAML file

I've been searching around for a bit and couldn't find anything that really helped me. Especially because sometimes things don't seem to be consistant.
I have the following YAML that I use to store data/ configuration stuff:
---
global:
name: Core Config
cfg_version: 0.0.1
databases:
main_database:
name: Main
path: ~/Documents/main.reevault
read_only: false
...
I know how to update fields with:
cfg = YAML.load_file("test.yml")
cfg['global']['name'] = 'New Name'
File.open("test.yml", "w"){ |f| YAML.dump(cfg, f) }
And that's essentially everybody on the internet talks about. However here is my problem: I want to dynamically be able to add new fields to that file. e.g. under the "databases" section have a "secondary_db" field with it's own name, path and read_only boolean. I would have expected to do that by just adding stuff to the hash:
cfg['global']['databases']['second_db'] = nil
cfg['global']['databases']['second_db']['name'] = "Secondary Database"
cfg['global']['databases']['second_db']['path'] = "http://someurl.remote/blob/db.reevault"
cfg['global']['databases']['second_db']['read_only'] = "true"
File.open("test.yml", "w"){ |f| YAML.dump(cfg, f) }
But I get this error:
`<main>': undefined method `[]=' for nil:NilClass (NoMethodError)
My question now is: how do I do this? Is there a way with the YAML interface? Or do I have to write stuff into the file manually? I would prefer something via the YAML module as it takes care of formatting/ indentation for me.
Hope someone can help me.
Yo have to initialize cfg['global']['database']['second_db'] to be a hash not nil. Try this cfg['global']['database']['second_db'] = {}

Resources