我正在寻找在嵌套字典中返回值的最有效方法。
在代码的这一点上,航空公司的名称(例如' Delta')和票价等级(例如' N')已经由用户输入并使用re进行验证。变量' airline_name'和' fare_class'已定义。有多个票价类包含相同的值; '台达' > ' F' ' J' > ' 2'
使用传入的变量名称' airline_name'搜索嵌套字典值(例如' 1.5')的最佳方法是什么?和' fare_class'?感谢您的反馈! (如果我需要重组我的词典,我也非常乐意)
`FARE_CLASS_MULTIPLIER_DICTIONARY = {
'Delta': {
('N'): 0,
('E', 'H', 'K', 'L', 'M', 'Q', 'S', 'T', 'U', 'V', 'W+', 'X'): 1,
('A', 'B', 'C', 'D', 'G', 'I', 'P', 'W', 'Y', 'Z'): 1.5,
('F', 'J'): 2
},
'United': {
('N'): 0,
('M', 'E', 'U', 'H', 'Q', 'V', 'W', 'S', 'T', 'L', 'K', 'G'): 1,
('Y', 'B'): 1.5,
('A', 'C', 'D', 'Z', 'P'): 2,
('F', 'J'): 3
},
'American': {
('A'): 0,
('H', 'K', 'M', 'L', 'V', 'G', 'S', 'N', 'Q', 'O'): 1,
('Y', 'W', 'P'): 1.5,
('A', 'D', 'I', 'R'): 2,
('F', 'J'): 3
}
}
答案 0 :(得分:0)
我会对其进行重组,因此每个fare_class都是给定航空公司字典中的关键字。因此,达美的字典将成为
/*
Plugin Name: Woocommerce Daily Restock
Plugin URI: http://no-uri.com
Description: Add a meta box into products to allow a daily restock of them
Version: 0.5
Author: Alex Vand
Text Domain: wc-daily-restock
Author URI: http://no-uri.com
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class WC_Daily_Restock {
/**
* Construct the class
*/
public function __construct(){
$this->init();
}
/**
* Init the class
*/
public function init(){
// Register the daily stock event
add_action( 'wcdr_daily_stock_event', array($this, 'increase_stock_daily'));
// Meta boxe init
add_action('add_meta_boxes', array($this, 'init_meta_boxe'));
add_action('save_post', array($this, 'save_meta_boxe'));
}
/**
* Function which register the event
*/
public function register_daily_stock_event() {
// Make sure this event hasn't been scheduled
if( !wp_next_scheduled( 'increase_stock_daily' ) ) {
// Schedule the event
wp_schedule_event( time(), 'daily', array($this, 'increase_stock_daily'));
}
}
/**
* Function to increase the stock of all products
*/
public function increase_stock_daily() {
/**
* Get all the products that have the meta value != empty and set the stock
*/
$args = array(
'post_type' => 'product',
'meta_query' => array(
'meta_key' => 'daily_restock_amount',
'meta_value' => '',
'compare' => '!='
)
);
// Get the products
$products = get_posts($args);
// If we are not getting any product, do nothing
if(empty($products)) {
return;
}
// Else, loop over the products
foreach($products as $product_post) {
// Get the restock value
$daily_restock_amount = get_post_meta($product_post->ID, 'daily_restock_amount', true);
$product = new WC_Product($product_post->ID);
// And if the stock is not equal to the restock value, restock it
if($product->get_total_stock() < $daily_restock_amount) {
$product->set_stock($daily_restock_amount);
}
}
}
/**
* Init the meta box where we can set the restock value
*/
function init_meta_boxe(){
add_meta_box(
'daily_restock_metabox',
__('Daily restock', 'wc-daily-restock'),
array($this, 'daily_restock_callback'),
array('product'),
'side',
'high'
);
}
/**
* The callback for the meta boxe content and input
*/
public function daily_restock_callback($post) {
// Add an nonce field so we can check for it later.
wp_nonce_field('daily_restock_check', 'daily_restock_check_value');
// Use get_post_meta to retrieve an existing value from the database.
$daily_restock_amount = get_post_meta($post -> ID, 'daily_restock_amount', true);
// Display the form, using the current value.
echo '<div>';
echo '<label for="custom_message">';
echo '<strong><p>'.__('Number of products to be back in stock').'</p></strong>';
echo '</label>';
echo '<input type="number" name="daily_restock_amount" value="'.esc_attr($daily_restock_amount).'" />';
echo '</div>';
}
/**
* Function called to save the meta boxe
*/
public function save_meta_boxe($post_id) {
// Check if our nonce is set.
if (!isset($_POST['daily_restock_check_value']))
return $post_id;
$nonce = $_POST['daily_restock_check_value'];
// Verify that the nonce is valid.
if (!wp_verify_nonce($nonce, 'daily_restock_check'))
return $post_id;
// If this is an autosave, our form has not been submitted,
// so we don't want to do anything.
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $post_id;
// Check the user's permissions.
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id))
return $post_id;
} else {
if (!current_user_can('edit_post', $post_id))
return $post_id;
}
// Sanitize the user input.
$data = sanitize_text_field($_POST['daily_restock_amount']);
// Update the meta field.
update_post_meta($post_id, 'daily_restock_amount', $data);
}
// Schedule the event
public static function activated() {
// Here you can replace 5:00 to what you want
// The condition is needed because if you activate the plugin while the time of today is passed, the event will not works.
// So for this case, we say "Don't worry, do that tomorrow at the same time". And after it will be daily
if( time() > strtotime( 'today 5:00' ) ) {
wp_schedule_event( strtotime( 'tomorrow 5:00' ), 'daily', 'wcdr_daily_stock_event' );
}
else {
wp_schedule_event( strtotime( 'today 5:00' ), 'daily', 'wcdr_daily_stock_event' );
}
}
public static function deactivated() {
wp_clear_scheduled_hook( 'wcdr_daily_stock_event' );
}
}
// Action on activation and deactivation
register_activation_hook(__FILE__, array( 'WC_Daily_Restock', 'activated' ) );
register_deactivation_hook( __FILE__, array( 'WC_Daily_Restock', 'deactivated' ) );
$dailyRestock = new WC_Daily_Restock();
然后可以使用
获得乘数'Delta': {
'N': 0,
'E': 1,
'H': 1,
'K': 1,
'L': 1,
'M': 1,
'Q': 1,
'S': 1,
'T': 1,
'U': 1,
'V': 1,
'W+': 1,
'X': 1,
'A': 1.5,
'B': 1.5,
'C': 1.5,
'D': 1.5,
'G': 1.5,
'I': 1.5,
'P': 1.5,
'W': 1.5,
'Y': 1.5,
'Z': 1.5,
'F': 2,
'J': 2,
},