python有一個小函數可以回傳當下的區域變數列表,不過如果在最外層就locals(),就是回傳全域變數們了!XDDD
locals()
locals()
from PIL import Image
bmp_image = Image.open( 'sample.bmp' )
for i_vertical in range(bmp_image.height):
line_horizon = [bmp_image.getpixel((i_horizon, i_vertical)) for i_horizon in range(bmp_image.width) ]
print(*line_horizon)
bmp_image.close()
effective python說用這樣比較快
from PIL import Image
bmp_image = Image.open( 'sample.bmp' )
line_horizon = (bmp_image.getpixel((i_horizon, i_vertical)) for i_vertical in range(bmp_image.height) for i_horizon in range(bmp_image.width))
for x in range(bmp_image.height):
for y in range(bmp_image.width):
print(next(line_horizon), ' ', end = '')
print('')
bmp_image.close()
執行結果:255 0 0 0 0 255 255 0 0 0 255 0 255 0 255 0 255 0 255 0 0 0 0 255 0 255 255 255 255 255
#include <string>
#include <map>
class Persion
{
std::string m_Name;
std::string m_HomeNumber;
public:
Persion();
explicit Persion(const std::string &name);
virtual ~Persion();
void SetName(const std::string &name);
std::string GetName() const;
void SetHomeNumber(const std::string &number);
std::string GetHomeNumber() const;
};
class PersionWithCell : public Persion
{
std::string m_CellNumber;
public:
PersionWithCell();
explicit PersionWithCell(const std::string &name);
void SetCellNumber(const std::string &number);
std::string GetCellNumber() const;
};
class PhoneBook
{
std::map<std::string, Persion*> m_PhoneBook;
public:
int GetSize() const;
void AddPerson(Persion *p);
void RemovePersion(const std::string &name);
Persion *FindPerison(const std::string &name);
};
#include "phonebook.h"
Persion::Persion():
m_Name(""), m_HomeNumber("")
{}
Persion::Persion(const std::string &name):
m_Name(name), m_HomeNumber("")
{}
void Persion::SetName(const std::string &name)
{
m_Name = name;
}
std::string Persion::GetName() const
{
return m_Name;
}
void Persion::SetHomeNumber(const std::string &number)
{
m_HomeNumber = number;
}
std::string Persion::GetHomeNumber() const
{
return m_HomeNumber;
}
Persion::~Persion()
{}
int PhoneBook::GetSize() const
{
return (int)m_PhoneBook.size();
}
void PhoneBook::AddPerson(Persion *p)
{
m_PhoneBook[p->GetName()] = p;
}
void PhoneBook::RemovePersion(const std::string &name)
{
m_PhoneBook.erase(name);
}
Persion *PhoneBook::FindPerison(const std::string &name)
{
return m_PhoneBook[name];
}
void PersionWithCell::SetCellNumber(const std::string &number)
{
m_CellNumber = number;
}
std::string PersionWithCell::GetCellNumber() const
{
return m_CellNumber;
}
PersionWithCell::PersionWithCell():m_CellNumber(""), Persion()
{}
PersionWithCell::PersionWithCell(const std::string &name):m_CellNumber(""), Persion(name)
{}
#include "phonebook.h"
#include <boost/python.hpp>
using namespace boost::python;
static std::string PrintPersion(const Persion &p)
{
std::ostringstream stream;
stream << p.GetName() << ": " << p.GetHomeNumber();
return stream.str();
}
std::ostream &operator<<(std::ostream &os, const Persion &p)
{
os << p.GetName() << ": " << p.GetHomeNumber();
return os;
}
BOOST_PYTHON_MODULE(phonebook)
{
class_<Persion>("Persion", init<>())
.def(init<std::string>())
.add_property("name", &Persion::GetName, &Persion::SetName)
.add_property("home_number", &Persion::GetHomeNumber, &Persion::SetHomeNumber)
.def("__str__", &PrintPersion)
.def(self_ns::str(self))
;
class_<PhoneBook>("PhoneBook")
.def("size", &PhoneBook::GetSize)
.def("add_persion", &PhoneBook::AddPerson)
.def("remove_persion", &PhoneBook::RemovePersion)
.def("find_persion", &PhoneBook::FindPerison,
return_value_policy<reference_existing_object>())
;
}
import debug.phonebook as phonebook
book = phonebook.PhoneBook()
p = phonebook.Persion()
p.name = "Chris"
p.home_number = '(123) 456-7890'
book.add_persion(p)
p = phonebook.Persion('Mary')
#p.name = 'Mary'
p.home_number = '(123) 456-7890'
book.add_persion(p)
print('No. of contacts =', book.size())
print(book.find_persion('Mary').home_number)
print(p)
print('--------')
import debug.phonebook as phonebook
p = phonebook.Persion('Mary')
print(p)
p.home_number = '(123) 456-7890'
print(p)
def persion_str(self):
return "Name: %s\nHome: %s" % (self.name, self.home_number)
phonebook.Persion.__str__ = persion_str
p = phonebook.Persion()
p.name = "Chris"
p.home_number = "(123) 456-7890"
print (p)
book = phonebook.PhoneBook()
class PyPersionWithCell(phonebook.Persion):
def get_cell_number(self):
return self.cell
def set_cell_number(self, n):
cell = n
celll_number = property(get_cell_number, set_cell_number)
p = PyPersionWithCell()
p.name = 'Martin'
p.home_number = '(123) 456-7890'
p.celll_number = '(123) 097-2134'
p2 = book.find_persion('Martin')
print(p2)
# mysite/settings.py
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates').replace('\\', '/'),
)
但是如果真的這樣寫的話,就會在Browser上發生,找不到Template的問題。出錯的檔案會提示在Exception Location上。如果它不是你寫的code,那就看看Exception Value。 |
# mysite/settings.py
#找到這一段
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'), #加入這一行
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
因為Django的1.7版和1.8版的差異,所以1.8版會找到TEMPLATES 並且要在DIR中加入路徑。