_id stringlengths 2 6 | title stringlengths 9 130 | partition stringclasses 3
values | text stringlengths 66 10.5k | language stringclasses 1
value | meta_information dict |
|---|---|---|---|---|---|
q14700 | RailsBestPractices.Analyzer.analyze_source_codes | train | def analyze_source_codes
@bar = ProgressBar.create(title: 'Source Code', total: parse_files.size * 3) if display_bar?
%w[lexical prepare review].each { |process| send(:process, process) }
@bar.finish if display_bar?
end | ruby | {
"resource": ""
} |
q14701 | Warden.Hooks._run_callbacks | train | def _run_callbacks(kind, *args) #:nodoc:
options = args.last # Last callback arg MUST be a Hash
send("_#{kind}").each do |callback, conditions|
invalid = conditions.find do |key, value|
value.is_a?(Array) ? !value.include?(options[key]) : (value != options[key])
end
callb... | ruby | {
"resource": ""
} |
q14702 | Warden.Hooks.after_failed_fetch | train | def after_failed_fetch(options = {}, method = :push, &block)
raise BlockNotGiven unless block_given?
_after_failed_fetch.send(method, [block, options])
end | ruby | {
"resource": ""
} |
q14703 | Warden.Hooks.before_logout | train | def before_logout(options = {}, method = :push, &block)
raise BlockNotGiven unless block_given?
_before_logout.send(method, [block, options])
end | ruby | {
"resource": ""
} |
q14704 | Warden.Hooks.on_request | train | def on_request(options = {}, method = :push, &block)
raise BlockNotGiven unless block_given?
_on_request.send(method, [block, options])
end | ruby | {
"resource": ""
} |
q14705 | Warden.Proxy.user | train | def user(argument = {})
opts = argument.is_a?(Hash) ? argument : { :scope => argument }
scope = (opts[:scope] ||= @config.default_scope)
if @users.has_key?(scope)
@users[scope]
else
unless user = session_serializer.fetch(scope)
run_callbacks = opts.fetch(:run_callback... | ruby | {
"resource": ""
} |
q14706 | Warden.Proxy.logout | train | def logout(*scopes)
if scopes.empty?
scopes = @users.keys
reset_session = true
end
scopes.each do |scope|
user = @users.delete(scope)
manager._run_callbacks(:before_logout, user, self, :scope => scope)
raw_session.delete("warden.user.#{scope}.session") unless ... | ruby | {
"resource": ""
} |
q14707 | Warden.Proxy._run_strategies_for | train | def _run_strategies_for(scope, args) #:nodoc:
self.winning_strategy = @winning_strategies[scope]
return if winning_strategy && winning_strategy.halted?
# Do not run any strategy if locked
return if @locked
if args.empty?
defaults = @config[:default_strategies]
strategie... | ruby | {
"resource": ""
} |
q14708 | Warden.Proxy._fetch_strategy | train | def _fetch_strategy(name, scope)
@strategies[scope][name] ||= if klass = Warden::Strategies[name]
klass.new(@env, scope)
elsif @config.silence_missing_strategies?
nil
else
raise "Invalid strategy #{name}"
end
end | ruby | {
"resource": ""
} |
q14709 | GooglePlaces.Request.parsed_response | train | def parsed_response
return @response.headers["location"] if @response.code >= 300 && @response.code < 400
raise APIConnectionError.new(@response) if @response.code >= 500 && @response.code < 600
case @response.parsed_response['status']
when 'OK', 'ZERO_RESULTS'
@response.parsed_response
... | ruby | {
"resource": ""
} |
q14710 | GooglePlaces.Client.spots | train | def spots(lat, lng, options = {})
options = @options.merge(options)
detail = options.delete(:detail)
collection_detail_level(
Spot.list(lat, lng, @api_key, options),
detail
)
end | ruby | {
"resource": ""
} |
q14711 | GooglePlaces.Client.spots_by_query | train | def spots_by_query(query, options = {})
options = @options.merge(options)
detail = options.delete(:detail)
collection_detail_level(
Spot.list_by_query(query, @api_key, options),
detail
)
end | ruby | {
"resource": ""
} |
q14712 | GooglePlaces.Client.spots_by_bounds | train | def spots_by_bounds(bounds, options = {})
options = @options.merge(options)
detail = options.delete(:detail)
collection_detail_level(
Spot.list_by_bounds(bounds, @api_key, options),
detail
)
end | ruby | {
"resource": ""
} |
q14713 | GooglePlaces.Client.spots_by_pagetoken | train | def spots_by_pagetoken(pagetoken, options = {})
options = @options.merge(options)
detail = options.delete(:detail)
collection_detail_level(
Spot.list_by_pagetoken(pagetoken, @api_key, options),
detail
)
end | ruby | {
"resource": ""
} |
q14714 | GooglePlaces.Client.spots_by_radar | train | def spots_by_radar(lat, lng, options = {})
options = @options.merge(options)
detail = options.delete(:detail)
collection_detail_level(
Spot.list_by_radar(lat, lng, @api_key, options),
detail
)
end | ruby | {
"resource": ""
} |
q14715 | GooglePlaces.Photo.fetch_url | train | def fetch_url(maxwidth, options = {})
language = options.delete(:language)
retry_options = options.delete(:retry_options) || {}
unless @fetched_url
@fetched_url = Request.photo_url(
:maxwidth => maxwidth,
:photoreference => @photo_reference,
:key => @api_key,
... | ruby | {
"resource": ""
} |
q14716 | Virtus.ConstMissingExtensions.const_missing | train | def const_missing(name)
Attribute::Builder.determine_type(name) or
Axiom::Types.const_defined?(name) && Axiom::Types.const_get(name) or
super
end | ruby | {
"resource": ""
} |
q14717 | Virtus.AttributeSet.each | train | def each
return to_enum unless block_given?
@index.each { |name, attribute| yield attribute if name.kind_of?(Symbol) }
self
end | ruby | {
"resource": ""
} |
q14718 | Virtus.AttributeSet.define_reader_method | train | def define_reader_method(attribute, method_name, visibility)
define_method(method_name) { attribute.get(self) }
send(visibility, method_name)
end | ruby | {
"resource": ""
} |
q14719 | Virtus.AttributeSet.define_writer_method | train | def define_writer_method(attribute, method_name, visibility)
define_method(method_name) { |value| attribute.set(self, value) }
send(visibility, method_name)
end | ruby | {
"resource": ""
} |
q14720 | Virtus.AttributeSet.get | train | def get(object)
each_with_object({}) do |attribute, attributes|
name = attribute.name
attributes[name] = object.__send__(name) if attribute.public_reader?
end
end | ruby | {
"resource": ""
} |
q14721 | Virtus.AttributeSet.set | train | def set(object, attributes)
coerce(attributes).each do |name, value|
writer_name = "#{name}="
if object.allowed_writer_methods.include?(writer_name)
object.__send__(writer_name, value)
end
end
end | ruby | {
"resource": ""
} |
q14722 | Virtus.AttributeSet.set_defaults | train | def set_defaults(object, filter = method(:skip_default?))
each do |attribute|
next if filter.call(object, attribute)
attribute.set_default_value(object)
end
end | ruby | {
"resource": ""
} |
q14723 | Virtus.TypeLookup.determine_type_from_primitive | train | def determine_type_from_primitive(primitive)
type = nil
descendants.select(&:primitive).reverse_each do |descendant|
descendant_primitive = descendant.primitive
next unless primitive <= descendant_primitive
type = descendant if type.nil? or type.primitive > descendant_primitive
... | ruby | {
"resource": ""
} |
q14724 | Virtus.TypeLookup.determine_type_from_string | train | def determine_type_from_string(string)
if string =~ TYPE_FORMAT and const_defined?(string, *EXTRA_CONST_ARGS)
const_get(string, *EXTRA_CONST_ARGS)
end
end | ruby | {
"resource": ""
} |
q14725 | Virtus.ModuleExtensions.extended | train | def extended(object)
super
@inclusions.each { |mod| object.extend(mod) }
define_attributes(object)
object.set_default_attributes
end | ruby | {
"resource": ""
} |
q14726 | Virtus.ModuleExtensions.included | train | def included(object)
super
if Class === object
@inclusions.reject do |mod|
object.ancestors.include?(mod)
end.each do |mod|
object.send(:include, mod)
end
define_attributes(object)
else
object.extend(ModuleExtensions)
ModuleExtension... | ruby | {
"resource": ""
} |
q14727 | Virtus.Builder.add_included_hook | train | def add_included_hook
with_hook_context do |context|
mod.define_singleton_method :included do |object|
Builder.pending << object unless context.finalize?
context.modules.each { |mod| object.send(:include, mod) }
object.define_singleton_method(:attribute, context.attribute_met... | ruby | {
"resource": ""
} |
q14728 | Virtus.Options.options | train | def options
accepted_options.each_with_object({}) do |option_name, options|
option_value = send(option_name)
options[option_name] = option_value unless option_value.nil?
end
end | ruby | {
"resource": ""
} |
q14729 | Virtus.Options.accept_options | train | def accept_options(*new_options)
add_accepted_options(new_options)
new_options.each { |option| define_option_method(option) }
descendants.each { |descendant| descendant.add_accepted_options(new_options) }
self
end | ruby | {
"resource": ""
} |
q14730 | ROTP.TOTP.verify | train | def verify(otp, drift_ahead: 0, drift_behind: 0, after: nil, at: Time.now)
timecodes = get_timecodes(at, drift_behind, drift_ahead)
timecodes = timecodes.select { |t| t > timecode(after) } if after
result = nil
timecodes.each do |t|
result = t * interval if super(otp, generate_otp(t))
... | ruby | {
"resource": ""
} |
q14731 | ROTP.TOTP.get_timecodes | train | def get_timecodes(at, drift_behind, drift_ahead)
now = timeint(at)
timecode_start = timecode(now - drift_behind)
timecode_end = timecode(now + drift_ahead)
(timecode_start..timecode_end).step(1).to_a
end | ruby | {
"resource": ""
} |
q14732 | ROTP.OTP.encode_params | train | def encode_params(uri, params)
params_str = String.new('?')
params.each do |k, v|
params_str << "#{k}=#{CGI.escape(v.to_s)}&" if v
end
params_str.chop!
uri + params_str
end | ruby | {
"resource": ""
} |
q14733 | ROTP.OTP.time_constant_compare | train | def time_constant_compare(a, b)
return false if a.empty? || b.empty? || a.bytesize != b.bytesize
l = a.unpack "C#{a.bytesize}"
res = 0
b.each_byte { |byte| res |= byte ^ l.shift }
res == 0
end | ruby | {
"resource": ""
} |
q14734 | ROTP.HOTP.verify | train | def verify(otp, counter, retries: 0)
counters = (counter..counter + retries).to_a
counters.find do |c|
super(otp, at(c))
end
end | ruby | {
"resource": ""
} |
q14735 | Fluent.GoogleCloudOutput.ec2_metadata | train | def ec2_metadata
raise "Called ec2_metadata with platform=#{@platform}" unless
@platform == Platform::EC2
unless @ec2_metadata
# See http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html
open('http://' + METADATA_SERVICE_ADDR +
'/latest/dynamic/in... | ruby | {
"resource": ""
} |
q14736 | Fluent.GoogleCloudOutput.set_required_metadata_variables | train | def set_required_metadata_variables
set_project_id
set_vm_id
set_vm_name
set_location
# All metadata parameters must now be set.
missing = []
missing << 'project_id' unless @project_id
if @platform != Platform::OTHER
missing << 'zone' unless @zone
missing... | ruby | {
"resource": ""
} |
q14737 | Fluent.GoogleCloudOutput.set_vm_id | train | def set_vm_id
@vm_id ||= fetch_gce_metadata('instance/id') if @platform == Platform::GCE
@vm_id ||= ec2_metadata['instanceId'] if @platform == Platform::EC2
rescue StandardError => e
@log.error 'Failed to obtain vm_id: ', error: e
end | ruby | {
"resource": ""
} |
q14738 | Fluent.GoogleCloudOutput.set_location | train | def set_location
# Response format: "projects/<number>/zones/<zone>"
@zone ||= fetch_gce_metadata('instance/zone').rpartition('/')[2] if
@platform == Platform::GCE
aws_location_key = if @use_aws_availability_zone
'availabilityZone'
else
... | ruby | {
"resource": ""
} |
q14739 | Fluent.GoogleCloudOutput.determine_agent_level_monitored_resource_via_legacy | train | def determine_agent_level_monitored_resource_via_legacy
resource = Google::Apis::LoggingV2::MonitoredResource.new(
labels: {})
resource.type = determine_agent_level_monitored_resource_type
resource.labels = determine_agent_level_monitored_resource_labels(
resource.type)
resource
... | ruby | {
"resource": ""
} |
q14740 | Fluent.GoogleCloudOutput.determine_agent_level_monitored_resource_type | train | def determine_agent_level_monitored_resource_type
case @platform
when Platform::OTHER
# Unknown platform will be defaulted to GCE instance.
return COMPUTE_CONSTANTS[:resource_type]
when Platform::EC2
return EC2_CONSTANTS[:resource_type]
when Platform::GCE
# Reso... | ruby | {
"resource": ""
} |
q14741 | Fluent.GoogleCloudOutput.determine_agent_level_monitored_resource_labels | train | def determine_agent_level_monitored_resource_labels(type)
case type
# GAE app.
when APPENGINE_CONSTANTS[:resource_type]
return {
'module_id' =>
fetch_gce_metadata('instance/attributes/gae_backend_name'),
'version_id' =>
fetch_gce_metadata('instance/a... | ruby | {
"resource": ""
} |
q14742 | Fluent.GoogleCloudOutput.determine_agent_level_common_labels | train | def determine_agent_level_common_labels
labels = {}
# User can specify labels via config. We want to capture those as well.
labels.merge!(@labels) if @labels
case @resource.type
# GAE, Cloud Dataflow, Cloud Dataproc and Cloud ML.
when APPENGINE_CONSTANTS[:resource_type],
... | ruby | {
"resource": ""
} |
q14743 | Fluent.GoogleCloudOutput.group_log_entries_by_tag_and_local_resource_id | train | def group_log_entries_by_tag_and_local_resource_id(chunk)
groups = {}
chunk.msgpack_each do |tag, time, record|
unless record.is_a?(Hash)
@log.warn 'Dropping log entries with malformed record: ' \
"'#{record.inspect}'. " \
'A log record should be in ... | ruby | {
"resource": ""
} |
q14744 | Fluent.GoogleCloudOutput.determine_group_level_monitored_resource_and_labels | train | def determine_group_level_monitored_resource_and_labels(tag,
local_resource_id)
resource = @resource.dup
resource.labels = @resource.labels.dup
common_labels = @common_labels.dup
# Change the resource type and set matched_regexp_group ... | ruby | {
"resource": ""
} |
q14745 | Fluent.GoogleCloudOutput.monitored_resource_from_local_resource_id | train | def monitored_resource_from_local_resource_id(local_resource_id)
return unless local_resource_id
if @enable_metadata_agent
@log.debug 'Calling metadata agent with local_resource_id: ' \
"#{local_resource_id}."
resource = query_metadata_agent_for_monitored_resource(
... | ruby | {
"resource": ""
} |
q14746 | Fluent.GoogleCloudOutput.determine_entry_level_monitored_resource_and_labels | train | def determine_entry_level_monitored_resource_and_labels(
group_level_resource, group_level_common_labels, record)
resource = group_level_resource.dup
resource.labels = group_level_resource.labels.dup
common_labels = group_level_common_labels.dup
case resource.type
# Cloud Functions.... | ruby | {
"resource": ""
} |
q14747 | Fluent.GoogleCloudOutput.query_metadata_agent | train | def query_metadata_agent(path)
url = "#{@metadata_agent_url}/#{path}"
@log.debug("Calling Metadata Agent: #{url}")
open(url) do |f|
response = f.read
parsed_hash = parse_json_or_nil(response)
if parsed_hash.nil?
@log.error 'Response from Metadata Agent is not in valid... | ruby | {
"resource": ""
} |
q14748 | Fluent.GoogleCloudOutput.parse_labels | train | def parse_labels(record)
payload_labels = record.delete(@labels_key)
return nil unless payload_labels
unless payload_labels.is_a?(Hash)
@log.error "Invalid value of '#{@labels_key}' in the payload: " \
"#{payload_labels}. Labels need to be a JSON object."
return nil
... | ruby | {
"resource": ""
} |
q14749 | Fluent.GoogleCloudOutput.sanitize_tag | train | def sanitize_tag(tag)
if @require_valid_tags &&
(!tag.is_a?(String) || tag == '' || convert_to_utf8(tag) != tag)
return nil
end
tag = convert_to_utf8(tag.to_s)
tag = '_' if tag == ''
tag
end | ruby | {
"resource": ""
} |
q14750 | Fluent.GoogleCloudOutput.delete_and_extract_labels | train | def delete_and_extract_labels(hash, label_map)
return {} if label_map.nil? || !label_map.is_a?(Hash) ||
hash.nil? || !hash.is_a?(Hash)
label_map.each_with_object({}) \
do |(original_label, new_label), extracted_labels|
value = hash.delete(original_label)
extracted_... | ruby | {
"resource": ""
} |
q14751 | Fluent.GoogleCloudOutput.convert_to_utf8 | train | def convert_to_utf8(input)
if @coerce_to_utf8
input.encode(
'utf-8',
invalid: :replace,
undef: :replace,
replace: @non_utf8_replacement_string)
else
begin
input.encode('utf-8')
rescue EncodingError
@log.error 'Encountered en... | ruby | {
"resource": ""
} |
q14752 | Fluent.GoogleCloudOutput.construct_k8s_resource_locally | train | def construct_k8s_resource_locally(local_resource_id)
return unless
/^
(?<resource_type>k8s_container)
\.(?<namespace_name>[0-9a-z-]+)
\.(?<pod_name>[.0-9a-z-]+)
\.(?<container_name>[0-9a-z-]+)$/x =~ local_resource_id ||
/^
(?<resource_type>k8s_pod... | ruby | {
"resource": ""
} |
q14753 | Monitoring.PrometheusMonitoringRegistry.counter | train | def counter(name, desc)
return @registry.counter(name, desc)
rescue Prometheus::Client::Registry::AlreadyRegisteredError
return @registry.get(name)
end | ruby | {
"resource": ""
} |
q14754 | ServiceWorker.Middleware.call | train | def call(env)
case env[REQUEST_METHOD]
when GET, HEAD
route_match = @router.match_route(env)
return respond_to_match(route_match, env) if route_match
end
@app.call(env)
end | ruby | {
"resource": ""
} |
q14755 | I18n::Tasks.UsedKeys.used_tree | train | def used_tree(key_filter: nil, strict: nil, include_raw_references: false)
src_tree = used_in_source_tree(key_filter: key_filter, strict: strict)
raw_refs, resolved_refs, used_refs = process_references(src_tree['used'].children)
raw_refs.leaves { |node| node.data[:ref_type] = :reference_usage }
... | ruby | {
"resource": ""
} |
q14756 | I18n::Tasks::Scanners.RubyKeyLiterals.strip_literal | train | def strip_literal(literal)
literal = literal[1..-1] if literal[0] == ':'
literal = literal[1..-2] if literal[0] == "'" || literal[0] == '"'
literal
end | ruby | {
"resource": ""
} |
q14757 | I18n::Tasks.MissingKeys.load_rails_i18n_pluralization! | train | def load_rails_i18n_pluralization!(locale)
path = File.join(Gem::Specification.find_by_name('rails-i18n').gem_dir, 'rails', 'pluralization', "#{locale}.rb")
eval(File.read(path), binding, path) # rubocop:disable Security/Eval
end | ruby | {
"resource": ""
} |
q14758 | I18n::Tasks.MissingKeys.missing_diff_tree | train | def missing_diff_tree(locale, compared_to = base_locale)
data[compared_to].select_keys do |key, _node|
locale_key_missing? locale, depluralize_key(key, compared_to)
end.set_root_key!(locale, type: :missing_diff).keys do |_key, node|
# change path and locale to base
data = { locale: l... | ruby | {
"resource": ""
} |
q14759 | I18n::Tasks.MissingKeys.missing_used_tree | train | def missing_used_tree(locale)
used_tree(strict: true).select_keys do |key, _node|
locale_key_missing?(locale, key)
end.set_root_key!(locale, type: :missing_used)
end | ruby | {
"resource": ""
} |
q14760 | I18n::Tasks::Scanners.PatternScanner.scan_file | train | def scan_file(path)
keys = []
text = read_file(path)
text.scan(@pattern) do |match|
src_pos = Regexp.last_match.offset(0).first
location = occurrence_from_position(path, text, src_pos, raw_key: strip_literal(match[0]))
next if exclude_line?(location.line, path)
key = ma... | ruby | {
"resource": ""
} |
q14761 | I18n::Tasks::Translators.GoogleTranslator.to_google_translate_compatible_locale | train | def to_google_translate_compatible_locale(locale)
return locale unless locale.include?('-') && !SUPPORTED_LOCALES_WITH_REGION.include?(locale)
locale.split('-', 2).first
end | ruby | {
"resource": ""
} |
q14762 | I18n::Tasks::Scanners.RubyAstScanner.scan_file | train | def scan_file(path)
@parser.reset
ast, comments = @parser.parse_with_comments(make_buffer(path))
results = @call_finder.collect_calls ast do |send_node, method_name|
send_node_to_key_occurrence(send_node, method_name)
end
magic_comments = comments.select { |comment| comment.text... | ruby | {
"resource": ""
} |
q14763 | I18n::Tasks::Scanners.RubyAstScanner.extract_hash_pair | train | def extract_hash_pair(node, key)
node.children.detect do |child|
next unless child.type == :pair
key_node = child.children[0]
%i[sym str].include?(key_node.type) && key_node.children[0].to_s == key
end
end | ruby | {
"resource": ""
} |
q14764 | I18n::Tasks::Scanners.RubyAstScanner.extract_array_as_string | train | def extract_array_as_string(node, array_join_with:, array_flatten: false, array_reject_blank: false)
children_strings = node.children.map do |child|
if %i[sym str int true false].include?(child.type) # rubocop:disable Lint/BooleanSymbol
extract_string child
else
# ignore dynami... | ruby | {
"resource": ""
} |
q14765 | I18n::Tasks::Data::Tree.Siblings.add_ancestors_that_only_contain_nodes! | train | def add_ancestors_that_only_contain_nodes!(nodes)
levels.reverse_each do |level_nodes|
level_nodes.each { |node| nodes << node if node.children? && node.children.all? { |c| nodes.include?(c) } }
end
end | ruby | {
"resource": ""
} |
q14766 | I18n::Tasks::Reports.Base.sort_by_attr! | train | def sort_by_attr!(objects, order = { locale: :asc, key: :asc })
order_keys = order.keys
objects.sort! do |a, b|
by = order_keys.detect { |k| a[k] != b[k] }
order[by] == :desc ? b[by] <=> a[by] : a[by] <=> b[by]
end
objects
end | ruby | {
"resource": ""
} |
q14767 | I18n::Tasks.Data.data | train | def data
@data ||= begin
data_config = (config[:data] || {}).deep_symbolize_keys
data_config[:base_locale] = base_locale
data_config[:locales] = config[:locales]
adapter_class = data_config[:adapter].presence || data_config[:class].presence || DATA_DEFAULTS[:adapter]
adapte... | ruby | {
"resource": ""
} |
q14768 | I18n::Tasks.References.merge_reference_trees | train | def merge_reference_trees(roots)
roots.inject(empty_forest) do |forest, root|
root.keys do |full_key, node|
if full_key == node.value.to_s
log_warn(
"Self-referencing key #{node.full_key(root: false).inspect} in #{node.data[:locale].inspect}"
)
end... | ruby | {
"resource": ""
} |
q14769 | I18n::Tasks::Scanners::Files.FileReader.read_file | train | def read_file(path)
result = nil
File.open(path, 'rb', encoding: 'UTF-8') { |f| result = f.read }
result
end | ruby | {
"resource": ""
} |
q14770 | Daru.CategoricalIndex.pos | train | def pos *indexes
positions = indexes.map do |index|
if include? index
@cat_hash[index]
elsif index.is_a?(Numeric) && index < @array.size
index
else
raise IndexError, "#{index.inspect} is neither a valid category"\
' nor a valid position'
en... | ruby | {
"resource": ""
} |
q14771 | Daru.CategoricalIndex.subset | train | def subset *indexes
positions = pos(*indexes)
new_index = positions.map { |pos| index_from_pos pos }
Daru::CategoricalIndex.new new_index.flatten
end | ruby | {
"resource": ""
} |
q14772 | Daru.CategoricalIndex.at | train | def at *positions
positions = preprocess_positions(*positions)
validate_positions(*positions)
if positions.is_a? Integer
index_from_pos(positions)
else
Daru::CategoricalIndex.new positions.map(&method(:index_from_pos))
end
end | ruby | {
"resource": ""
} |
q14773 | Daru.MultiIndex.validate_name | train | def validate_name names, levels
error_msg = "'names' and 'levels' should be of same size. Size of the "\
"'name' array is #{names.size} and size of the MultiIndex 'levels' and "\
"'labels' is #{labels.size}."
suggestion_msg = "If you don\'t want to set name for particular level " \
"(say l... | ruby | {
"resource": ""
} |
q14774 | Daru.Vector.[] | train | def [](*input_indexes)
# Get array of positions indexes
positions = @index.pos(*input_indexes)
# If one object is asked return it
return @data[positions] if positions.is_a? Numeric
# Form a new Vector using positional indexes
Daru::Vector.new(
positions.map { |loc| @data[lo... | ruby | {
"resource": ""
} |
q14775 | Daru.Vector.at | train | def at *positions
# to be used to form index
original_positions = positions
positions = coerce_positions(*positions)
validate_positions(*positions)
if positions.is_a? Integer
@data[positions]
else
values = positions.map { |pos| @data[pos] }
Daru::Vector.new v... | ruby | {
"resource": ""
} |
q14776 | Daru.Vector.set_at | train | def set_at positions, val
validate_positions(*positions)
positions.map { |pos| @data[pos] = val }
update_position_cache
end | ruby | {
"resource": ""
} |
q14777 | Daru.Vector.concat | train | def concat element, index
raise IndexError, 'Expected new unique index' if @index.include? index
@index |= [index]
@data[@index[index]] = element
update_position_cache
end | ruby | {
"resource": ""
} |
q14778 | Daru.Vector.cast | train | def cast opts={}
dt = opts[:dtype]
raise ArgumentError, "Unsupported dtype #{opts[:dtype]}" unless %i[array nmatrix gsl].include?(dt)
@data = cast_vector_to dt unless @dtype == dt
end | ruby | {
"resource": ""
} |
q14779 | Daru.Vector.delete_at | train | def delete_at index
@data.delete_at @index[index]
@index = Daru::Index.new(@index.to_a - [index])
update_position_cache
end | ruby | {
"resource": ""
} |
q14780 | Daru.Vector.index_of | train | def index_of element
case dtype
when :array then @index.key(@data.index { |x| x.eql? element })
else @index.key @data.index(element)
end
end | ruby | {
"resource": ""
} |
q14781 | Daru.Vector.uniq | train | def uniq
uniq_vector = @data.uniq
new_index = uniq_vector.map { |element| index_of(element) }
Daru::Vector.new uniq_vector, name: @name, index: new_index, dtype: @dtype
end | ruby | {
"resource": ""
} |
q14782 | Daru.Vector.sort_by_index | train | def sort_by_index opts={}
opts = {ascending: true}.merge(opts)
_, new_order = resort_index(@index.each_with_index, opts).transpose
reorder new_order
end | ruby | {
"resource": ""
} |
q14783 | Daru.Vector.recode! | train | def recode! dt=nil, &block
return to_enum(:recode!) unless block_given?
@data.map!(&block).data
@data = cast_vector_to(dt || @dtype)
self
end | ruby | {
"resource": ""
} |
q14784 | Daru.Vector.delete_if | train | def delete_if
return to_enum(:delete_if) unless block_given?
keep_e, keep_i = each_with_index.reject { |n, _i| yield(n) }.transpose
@data = cast_vector_to @dtype, keep_e
@index = Daru::Index.new(keep_i)
update_position_cache
self
end | ruby | {
"resource": ""
} |
q14785 | Daru.Vector.verify | train | def verify
(0...size)
.map { |i| [i, @data[i]] }
.reject { |_i, val| yield(val) }
.to_h
end | ruby | {
"resource": ""
} |
q14786 | Daru.Vector.lag | train | def lag k=1
case k
when 0 then dup
when 1...size
copy([nil] * k + data.to_a)
when -size..-1
copy(data.to_a[k.abs...size])
else
copy([])
end
end | ruby | {
"resource": ""
} |
q14787 | Daru.Vector.to_matrix | train | def to_matrix axis=:horizontal
if axis == :horizontal
Matrix[to_a]
elsif axis == :vertical
Matrix.columns([to_a])
else
raise ArgumentError, "axis should be either :horizontal or :vertical, not #{axis}"
end
end | ruby | {
"resource": ""
} |
q14788 | Daru.Vector.to_nmatrix | train | def to_nmatrix axis=:horizontal
unless numeric? && !include?(nil)
raise ArgumentError, 'Can not convert to nmatrix'\
'because the vector is numeric'
end
case axis
when :horizontal
NMatrix.new [1, size], to_a
when :vertical
NMatrix.new [size, 1], to_a
... | ruby | {
"resource": ""
} |
q14789 | Daru.Vector.object_summary | train | def object_summary
nval = count_values(*Daru::MISSING_VALUES)
summary = "\n factors: #{factors.to_a.join(',')}" \
"\n mode: #{mode.to_a.join(',')}" \
"\n Distribution\n"
data = frequencies.sort.each_with_index.map do |v, k|
[k, v, '%0.2f%%' % ((nval.zero? ? ... | ruby | {
"resource": ""
} |
q14790 | Daru.Vector.numeric_summary | train | def numeric_summary
summary = "\n median: #{median}" +
"\n mean: %0.4f" % mean
if sd
summary << "\n std.dev.: %0.4f" % sd +
"\n std.err.: %0.4f" % se
end
if count_values(*Daru::MISSING_VALUES).zero?
summary << "\n skew: %0.4f" % skew +
... | ruby | {
"resource": ""
} |
q14791 | Daru.Vector.inspect | train | def inspect spacing=20, threshold=15
row_headers = index.is_a?(MultiIndex) ? index.sparse_tuples : index.to_a
"#<#{self.class}(#{size})#{':category' if category?}>\n" +
Formatters::Table.format(
to_a.lazy.map { |v| [v] },
headers: @name && [@name],
row_headers: row_hea... | ruby | {
"resource": ""
} |
q14792 | Daru.Vector.reindex! | train | def reindex! new_index
values = []
each_with_index do |val, i|
values[new_index[i]] = val if new_index.include?(i)
end
values.fill(nil, values.size, new_index.size - values.size)
@data = cast_vector_to @dtype, values
@index = new_index
update_position_cache
sel... | ruby | {
"resource": ""
} |
q14793 | Daru.Vector.only_valid | train | def only_valid as_a=:vector, _duplicate=true
# FIXME: Now duplicate is just ignored.
# There are no spec that fail on this case, so I'll leave it
# this way for now - zverok, 2016-05-07
new_index = @index.to_a - indexes(*Daru::MISSING_VALUES)
new_vector = new_index.map { |idx| self[id... | ruby | {
"resource": ""
} |
q14794 | Daru.Vector.only_numerics | train | def only_numerics
numeric_indexes =
each_with_index
.select { |v, _i| v.is_a?(Numeric) || v.nil? }
.map(&:last)
self[*numeric_indexes]
end | ruby | {
"resource": ""
} |
q14795 | Daru.Vector.db_type | train | def db_type
# first, detect any character not number
case
when @data.any? { |v| v.to_s =~ DATE_REGEXP }
'DATE'
when @data.any? { |v| v.to_s =~ /[^0-9e.-]/ }
'VARCHAR (255)'
when @data.any? { |v| v.to_s =~ /\./ }
'DOUBLE'
else
'INTEGER'
end
en... | ruby | {
"resource": ""
} |
q14796 | Daru.Vector.to_category | train | def to_category opts={}
dv = Daru::Vector.new to_a, type: :category, name: @name, index: @index
dv.ordered = opts[:ordered] || false
dv.categories = opts[:categories] if opts[:categories]
dv
end | ruby | {
"resource": ""
} |
q14797 | Daru.Vector.cut | train | def cut partitions, opts={}
close_at, labels = opts[:close_at] || :right, opts[:labels]
partitions = partitions.to_a
values = to_a.map { |val| cut_find_category partitions, val, close_at }
cats = cut_categories(partitions, close_at)
dv = Daru::Vector.new values,
index: @index,
... | ruby | {
"resource": ""
} |
q14798 | Daru.Vector.valid_value? | train | def valid_value?(v)
v.respond_to?(:nan?) && v.nan? || v.nil? ? false : true
end | ruby | {
"resource": ""
} |
q14799 | Daru.Vector.prepare_bootstrap | train | def prepare_bootstrap(estimators)
h_est = estimators
h_est = [h_est] unless h_est.is_a?(Array) || h_est.is_a?(Hash)
if h_est.is_a? Array
h_est = h_est.map do |est|
[est, ->(v) { Daru::Vector.new(v).send(est) }]
end.to_h
end
bss = h_est.keys.map { |v| [v, []] }.to... | ruby | {
"resource": ""
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.