如何理解extract_options!

本文介绍了 Rails 框架中 args.extract_options! 方法的功能及应用,通过实例展示了如何区分参数和选项,并提供了一个实用示例程序来帮助理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当你在阅读程序的时候,你经常碰到下面的程序


def my_method(*args)
  options = args.extract_options!
  puts "Arguments:  #{args.inspect}"
  puts "Options:    #{options.inspect}"
end

有可能很多人看不多


现在给两个列子


1.   

my_method(1, 2)
# Arguments:  [1, 2]
# Options:    {}


2. 

my_method(1, 2, :a => :b)
# Arguments:  [1, 2]
# Options:    {:a=>:b}


现在看懂了吗? 不过一定要记住这个在ruby中没有,而是存在于rails中。 



好了,让我们用这个简单的功能写个程序吧


def g_html(sym, *args)
   options = args.extract_options!
   if options[:class]
     "<#{sym} class='#{options[:class]}'>#{args.first}</#{sym}>"
   else
     "<#{sym}>#{args.first}</#{sym}>" if args.first.present?
   end
 end



详细解释一下这段代码,每一句都要进行注解:tgt = f'/kaggle/working/{dataset}-{scene}' # Generate a simple reconstruction with SIFT (https://en.wikipedia.org/wiki/Scale-invariant_feature_transform). if not os.path.isdir(tgt): os.makedirs(f'{tgt}/bundle') os.system(f'cp -r {src}/images {tgt}/images') database_path = f'{tgt}/database.db' sift_opt = pycolmap.SiftExtractionOptions() sift_opt.max_image_size = 1500 # Extract features at low resolution could significantly reduce the overall accuracy sift_opt.max_num_features = 8192 # Generally more features is better, even if behond a certain number it doesn't help incresing accuracy sift_opt.upright = True # rotation invariance device = 'cpu' t = time() pycolmap.extract_features(database_path, f'{tgt}/images', sift_options=sift_opt, verbose=True) print(len(os.listdir(f'{tgt}/images'))) print('TIMINGS --- Feature extraction', time() - t) t = time() matching_opt = pycolmap.SiftMatchingOptions() matching_opt.max_ratio = 0.85 # Ratio threshold significantly influence the performance of the feature extraction method. It varies depending on the local feature but also on the image type # matching_opt.max_distance = 0.7 matching_opt.cross_check = True matching_opt.max_error = 1.0 # The ransac error threshold could help to exclude less accurate tie points pycolmap.match_exhaustive(database_path, sift_options=matching_opt, device=device, verbose=True) print('TIMINGS --- Feature matching', time() - t) t = time() mapper_options = pycolmap.IncrementalMapperOptions() mapper_options.extract_colors = False mapper_options.min_model_size = 3 # Sometimes you want to impose the first image pair for initialize the incremental reconstruction mapper_options.init_image_id1 = -1 mapper_options.init_image_id2 = -1 # Choose which interior will be refined during BA mapper_options.ba_refine_focal_length = True mapper_options.ba_refine_principal_point = True mapper_options.ba_refine_extra_params = True maps = pycolmap.incremental_mapping(database_path=database_path, image_path=f'{tgt}/images', output_path=f'{tgt}/bundle', options=mapper_options) print('TIMINGS --- Mapping', time() - t)
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值