如何修复django-oscar运送UnboundLocalError?

时间:2019-04-09 22:04:39

标签: django django-oscar

我想将运输方法添加到django-oscar,但是即使我在文档页面上做了所有操作,也遇到了UnboundLocalError错误。

Request Method: GET
Request URL:    http://127.0.0.1:8000/checkout/shipping-address/
Django Version: 2.1.7
Exception Type: UnboundLocalError
Exception Value:    
local variable 'methods' referenced before assignment

repository.py

from oscar.apps.shipping import repository
from . import methods

class Repository(repository.Repository):

    def get_available_shipping_methods(self, basket, user=None, shipping_addr=None, request=None, **kwargs):
        methods = (methods.Standard(),)
        if shipping_addr and shipping_addr.country.code == 'GB':
            # Express is only available in the UK
            methods = (methods.Standard(), methods.Express())
        return methods

methods.py

from oscar.apps.shipping import methods
from oscar.core import prices
from decimal import Decimal as D

class Standard(methods.Base):
    code = 'standard'
    name = 'Shipping (Standard)'

    def calculate(self, basket):
        return prices.Price(
            currency=basket.currency,
            excl_tax=D('5.00'), incl_tax=D('5.00'))

class Express(methods.Base):
    code = 'express'
    name = 'Shipping (Express)'

    def calculate(self, basket):
        return prices.Price(
            currency=basket.currency,
            excl_tax=D('4.00'), incl_tax=D('4.00'))

1 个答案:

答案 0 :(得分:1)

我可以在文档中看到它,但是他们看起来里面有个错误。

使用import gym import numpy as np import tensorflow as tf from collections import deque # ==== import below from my repo ==== from common.wrappers import MyWrapper # just a wrapper to set a reward at the terminal state -1 from common.params import Parameters # params for training from common.memory import ReplayBuffer # Experience Replay Buffer tf.enable_eager_execution() class Model(tf.keras.Model): def __init__(self, num_action): super(Model, self).__init__() self.dense1 = tf.keras.layers.Dense(16, activation='relu') self.dense2 = tf.keras.layers.Dense(16, activation='relu') self.dense3 = tf.keras.layers.Dense(16, activation='relu') self.pred = tf.keras.layers.Dense(num_action, activation='softmax') def call(self, inputs): x = self.dense1(inputs) x = self.dense2(x) x = self.dense3(x) pred = self.pred(x) return pred class DQN: """ On policy DQN """ def __init__(self, num_action): self.num_action = num_action self.model = Model(num_action) self.optimizer = tf.train.AdamOptimizer() def predict(self, state): return self.model(tf.convert_to_tensor(state[None, :], dtype=tf.float32)).numpy()[0] def update(self, state, action, target): # target: R + gamma * Q(s',a') # calculate Q(s,a) q_values = self.predict(state) actions_one_hot = tf.one_hot(action, self.num_action, 1.0, 0.0) action_probs = tf.reduce_sum(actions_one_hot * q_values, reduction_indices=-1) # Minibatch MSE => (1/batch_size) * (R + gamma * Q(s',a') - Q(s,a))^2 loss = tf.reduce_mean(tf.squared_difference(target, action_probs)) return loss if __name__ == '__main__': reward_buffer = deque(maxlen=5) env = MyWrapper(gym.make("CartPole-v0")) replay_buffer = ReplayBuffer(5000) params = Parameters(mode="CartPole") agent = DQN(env.action_space.n) for i in range(2000): state = env.reset() total_reward = 0 for t in range(210): # env.render() action = np.argmax(agent.predict(state)) # behave greedily next_state, reward, done, info = env.step(action) replay_buffer.add(state, action, reward, next_state, done) total_reward += reward state = next_state if done: print("Episode {0} finished after {1} timesteps".format(i, t + 1)) if i > 10: print("Update") with tf.GradientTape() as tape: states, actions, rewards, next_states, dones = replay_buffer.sample(params.batch_size) next_Q = agent.predict(next_states) Y = rewards + params.gamma * np.max(next_Q, axis=1) * np.logical_not(dones) loss = agent.update(states, actions, Y) print(loss) grads = tape.gradient(loss, agent.model.trainable_weights) # ==== THIS RETURNS ONLY NONE ==== print(grads) agent.optimizer.apply_gradients(zip(grads, agent.model.trainable_weights)) break # store the episode reward reward_buffer.append(total_reward) # check the stopping condition if np.mean(reward_buffer) > 195: print("GAME OVER!!") break env.close() import gym import numpy as np import tensorflow as tf from collections import deque # ==== import below from my repo ==== from common.wrappers import MyWrapper # just a wrapper to set a reward at the terminal state -1 from common.params import Parameters # params for training from common.memory import ReplayBuffer # Experience Replay Buffer tf.enable_eager_execution() class Model(tf.keras.Model): def __init__(self, num_action): super(Model, self).__init__() self.dense1 = tf.keras.layers.Dense(16, activation='relu') self.dense2 = tf.keras.layers.Dense(16, activation='relu') self.dense3 = tf.keras.layers.Dense(16, activation='relu') self.pred = tf.keras.layers.Dense(num_action, activation='softmax') def call(self, inputs): x = self.dense1(inputs) x = self.dense2(x) x = self.dense3(x) pred = self.pred(x) return pred class DQN: """ On policy DQN """ def __init__(self, num_action): self.num_action = num_action self.model = Model(num_action) self.optimizer = tf.train.AdamOptimizer() def predict(self, state): return self.model(tf.convert_to_tensor(state[None, :], dtype=tf.float32)).numpy()[0] def update(self, state, action, target): # target: R + gamma * Q(s',a') # calculate Q(s,a) q_values = self.predict(state) actions_one_hot = tf.one_hot(action, self.num_action, 1.0, 0.0) action_probs = tf.reduce_sum(actions_one_hot * q_values, reduction_indices=-1) # Minibatch MSE => (1/batch_size) * (R + gamma * Q(s',a') - Q(s,a))^2 loss = tf.reduce_mean(tf.squared_difference(target, action_probs)) return loss if __name__ == '__main__': reward_buffer = deque(maxlen=5) env = MyWrapper(gym.make("CartPole-v0")) replay_buffer = ReplayBuffer(5000) params = Parameters(mode="CartPole") agent = DQN(env.action_space.n) for i in range(2000): state = env.reset() total_reward = 0 for t in range(210): # env.render() action = np.argmax(agent.predict(state)) # behave greedily next_state, reward, done, info = env.step(action) replay_buffer.add(state, action, reward, next_state, done) total_reward += reward state = next_state if done: print("Episode {0} finished after {1} timesteps".format(i, t + 1)) if i > 10: print("Update") with tf.GradientTape() as tape: states, actions, rewards, next_states, dones = replay_buffer.sample(params.batch_size) next_Q = agent.predict(next_states) Y = rewards + params.gamma * np.max(next_Q, axis=1) * np.logical_not(dones) loss = agent.update(states, actions, Y) print(loss) grads = tape.gradient(loss, agent.model.trainable_weights) # ==== THIS RETURNS ONLY NONE ==== print(grads) agent.optimizer.apply_gradients(zip(grads, agent.model.trainable_weights)) break # store the episode reward reward_buffer.append(total_reward) # check the stopping condition if np.mean(reward_buffer) > 195: print("GAME OVER!!") break env.close() import gym import numpy as np import tensorflow as tf from collections import deque # ==== import below from my repo ==== from common.wrappers import MyWrapper # just a wrapper to set a reward at the terminal state -1 from common.params import Parameters # params for training from common.memory import ReplayBuffer # Experience Replay Buffer tf.enable_eager_execution() class Model(tf.keras.Model): def __init__(self, num_action): super(Model, self).__init__() self.dense1 = tf.keras.layers.Dense(16, activation='relu') self.dense2 = tf.keras.layers.Dense(16, activation='relu') self.dense3 = tf.keras.layers.Dense(16, activation='relu') self.pred = tf.keras.layers.Dense(num_action, activation='softmax') def call(self, inputs): x = self.dense1(inputs) x = self.dense2(x) x = self.dense3(x) pred = self.pred(x) return pred class DQN: """ On policy DQN """ def __init__(self, num_action): self.num_action = num_action self.model = Model(num_action) self.optimizer = tf.train.AdamOptimizer() def predict(self, state): return self.model(tf.convert_to_tensor(state[None, :], dtype=tf.float32)).numpy()[0] def update(self, state, action, target): # target: R + gamma * Q(s',a') # calculate Q(s,a) q_values = self.predict(state) actions_one_hot = tf.one_hot(action, self.num_action, 1.0, 0.0) action_probs = tf.reduce_sum(actions_one_hot * q_values, reduction_indices=-1) # Minibatch MSE => (1/batch_size) * (R + gamma * Q(s',a') - Q(s,a))^2 loss = tf.reduce_mean(tf.squared_difference(target, action_probs)) return loss if __name__ == '__main__': reward_buffer = deque(maxlen=5) env = MyWrapper(gym.make("CartPole-v0")) replay_buffer = ReplayBuffer(5000) params = Parameters(mode="CartPole") agent = DQN(env.action_space.n) for i in range(2000): state = env.reset() total_reward = 0 for t in range(210): # env.render() action = np.argmax(agent.predict(state)) # behave greedily next_state, reward, done, info = env.step(action) replay_buffer.add(state, action, reward, next_state, done) total_reward += reward state = next_state if done: print("Episode {0} finished after {1} timesteps".format(i, t + 1)) if i > 10: print("Update") with tf.GradientTape() as tape: states, actions, rewards, next_states, dones = replay_buffer.sample(params.batch_size) next_Q = agent.predict(next_states) Y = rewards + params.gamma * np.max(next_Q, axis=1) * np.logical_not(dones) loss = agent.update(states, actions, Y) print(loss) grads = tape.gradient(loss, agent.model.trainable_weights) # ==== THIS RETURNS ONLY NONE ==== print(grads) agent.optimizer.apply_gradients(zip(grads, agent.model.trainable_weights)) break # store the episode reward reward_buffer.append(total_reward) # check the stopping condition if np.mean(reward_buffer) > 195: print("GAME OVER!!") break env.close() import gym import numpy as np import tensorflow as tf from collections import deque # ==== import below from my repo ==== from common.wrappers import MyWrapper # just a wrapper to set a reward at the terminal state -1 from common.params import Parameters # params for training from common.memory import ReplayBuffer # Experience Replay Buffer tf.enable_eager_execution() class Model(tf.keras.Model): def __init__(self, num_action): super(Model, self).__init__() self.dense1 = tf.keras.layers.Dense(16, activation='relu') self.dense2 = tf.keras.layers.Dense(16, activation='relu') self.dense3 = tf.keras.layers.Dense(16, activation='relu') self.pred = tf.keras.layers.Dense(num_action, activation='softmax') def call(self, inputs): x = self.dense1(inputs) x = self.dense2(x) x = self.dense3(x) pred = self.pred(x) return pred class DQN: """ On policy DQN """ def __init__(self, num_action): self.num_action = num_action self.model = Model(num_action) self.optimizer = tf.train.AdamOptimizer() def predict(self, state): return self.model(tf.convert_to_tensor(state[None, :], dtype=tf.float32)).numpy()[0] def update(self, state, action, target): # target: R + gamma * Q(s',a') # calculate Q(s,a) q_values = self.predict(state) actions_one_hot = tf.one_hot(action, self.num_action, 1.0, 0.0) action_probs = tf.reduce_sum(actions_one_hot * q_values, reduction_indices=-1) # Minibatch MSE => (1/batch_size) * (R + gamma * Q(s',a') - Q(s,a))^2 loss = tf.reduce_mean(tf.squared_difference(target, action_probs)) return loss if __name__ == '__main__': reward_buffer = deque(maxlen=5) env = MyWrapper(gym.make("CartPole-v0")) replay_buffer = ReplayBuffer(5000) params = Parameters(mode="CartPole") agent = DQN(env.action_space.n) for i in range(2000): state = env.reset() total_reward = 0 for t in range(210): # env.render() action = np.argmax(agent.predict(state)) # behave greedily next_state, reward, done, info = env.step(action) replay_buffer.add(state, action, reward, next_state, done) total_reward += reward state = next_state if done: print("Episode {0} finished after {1} timesteps".format(i, t + 1)) if i > 10: print("Update") with tf.GradientTape() as tape: states, actions, rewards, next_states, dones = replay_buffer.sample(params.batch_size) next_Q = agent.predict(next_states) Y = rewards + params.gamma * np.max(next_Q, axis=1) * np.logical_not(dones) loss = agent.update(states, actions, Y) print(loss) grads = tape.gradient(loss, agent.model.trainable_weights) # ==== THIS RETURNS ONLY NONE ==== print(grads) agent.optimizer.apply_gradients(zip(grads, agent.model.trainable_weights)) break # store the episode reward reward_buffer.append(total_reward) # check the stopping condition if np.mean(reward_buffer) > 195: print("GAME OVER!!") break env.close() ,您实际上正在研究范围问题。一个非常简单的例子是;

UnboundLocalError

x = 10 def foo(): x += 1 print x foo() 无法执行x foo时。因此,请稍微更改导入以避免这种情况;

foo