python etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
python etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

12 Aralık 2022 Pazartesi

Python Notlarım

apt vs pip3
Elimizde şöyle iki tane seçenek olsun. apt ile kurulum tercih edilmeli
pip3 install setuptools

apt install python3-setuptools
pip3 komutu
intall seçeneği
Örnek
requirements.txt şöyle olsun
PyYAML
jsonschema
jinja2
Şöyle yaparız
pip3 install -r requirements.txt

10 Ağustos 2016 Çarşamba

python'dan C++ Kullanmak

BOOST_PYTHON_IMPORT
Şöyle yaparız.
class A {
    ... // constructor is omitted 
public:
    const std::string str1;
};
class B {
public:
    std::shared_ptr<A> a;
}

BOOST_PYTHON_IMPORT(wrapped) {

    class_<A, std::shared_ptr<A>>("APy")
    .def_readonly("str1", &A::str1);

    class_<B>("BPy")
    .def_readwrite("a", &B::a);
}
Python'da kullanmak için şöyle yaparız.
import wrapped as wr
b = wr.BPy()
s1 = b.a.str1 // APy wrapper created
s2 = b.a.str1 // new APy wrapper created even though object is the same

class_ Sınıfı
C++ kodunu python'dan çağırmak için kullanılır

Constructor
Elimizde şöyle bir sınıf olsun
class Example
{
public:
  Example()
  {
    std::cout << "hello\n";
  }
  Example(const Example& e)
  {
    std::cout << "copy\n";
    counter++;
  }
  ~Example()
  {
    std::cout << "bye\n";
  }
  Example& count()
  {
    std::cout << "Count: " << counter << std::endl;
    return *this;
  }
  static int counter;
};
int Example::counter = 0;
Şöyle yaparız.
class_<Example>("Example", init<>())
def metodu
Açıklaması şöyle. C++ kodunda & kullanılsa bile nesnelerin copy constructor metodunun çağrılmasını sağlar.
copy_non_const_reference is a model of ResultConverterGenerator which can be used to wrap C++ functions returning a reference-to-non-const type such that the referenced value is copied into a new Python object.
Şöyle yaparız.
usingclass_ c;
c.def("count", &Example::count,
 return_value_policy<copy_non_const_reference>());
Bu sınıfı Python'da kullanmak için şöyle yaparız.
obj=Example()
obj.count().count()

8 Ağustos 2016 Pazartesi

boost python

Giriş
Şu satırları dahil ederiz.
#include <boost/python.hpp>
#include <Python.h>
Rahat kullanmak için şu satırı dahil ederiz.
using namespace boost::python;
class_ Sınıfı
python'dan C++ Kullanmak yazısına taşıdım.

stl_iterator Sınıfı
Şu satırı dahil ederiz.
#include <boost/python/stl_iterator.hpp>
python tarafından döndürülen bir liste olsun. Şöyle dolaşırız.
list records = call_method<list>(...);
stl_input_iterator<tuple> end;
for (stl_input_iterator<tuple> itr(records); itr != end;++itr)
{
  auto record = *itr;
  std::string tick = call_method<std::string>(record.ptr(), "__getitem__", 0);
  ...
}
Free Style metodlar
exec metodu
Şöyle yaparız.
namespace py = boost::python;

int main()
{
  // Must be called before any boost::python functions
  Py_Initialize();
  // import the main module
  py::object main_module = py::import("__main__");
  // load the dictionary object out of the main module
  py::object main_namespace = main_module.attr("__dict__");
  // run simple code within the main namespace using the boost::python::exec 
  //  function
  py::exec("print ('Hello, world')", main_namespace);
  // any valid Python will execute
  py::exec("print ('Hello, world')[3:5]", main_namespace);
}