cpython

Possible mixed indentation in Python?

萝らか妹 提交于 2019-12-01 21:18:50
Looking at this question, I tried OP's the code on my machine. Here are a text version and a screenshot: What just happened? This supposed to be a square function, and it is implemented correctly. To be sure, I copy-pasted the code, and tried it again: Well, I can't see any difference between these versions of square , but only the latter works. The only reason I can think of is that I may have mixed tabs and spaces, so the return statement is actually indented, and so the loop is executed exactly once. But I could not reproduce it, and it looks like an unbelievable flaw in the interpreter's

mac上安装python3的cx_Oracle数据库驱动

不羁岁月 提交于 2019-12-01 15:21:52
问题 使用Python3 for mac上面的cx_Oracle数据库驱动 步骤 下载Oracle的Instant Client 程序包 Instant Client 下载适用于 Mac OS X (Intel x86) 这里版本的选择,应该根据你访问的oracle库的版本来选择。 配置环境变量 ORACLE_HOME=/usr/local/oracle/instantclient_11_2 LD_LIBRARY_PATH=$ORACLE_HOME VERSION=11.2.0.3.0 ARCH=x86_64 DYLD_LIBRARY_PATH=/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/clidriver/lib:$ORACLE_HOME export ORACLE_HOME export LD_LIBRARY_PATH export VERSION export ARCH export DYLD_LIBRARY_PATH **Note:**这里的DYLD_LIBRARY_PATH需要留意,如果这里配置失误,会导致Python找不到相关库,如下错误: >>> import cx_Oracle Traceback (most recent call last): File

CPython: Why does += for strings change the id of string variable

江枫思渺然 提交于 2019-12-01 09:05:23
Cpython optimizes string increment operations,When initializing memory for a string, the program leaves extra expansion space for it,so, when incrementing, the original string is not copied to the new location. my question is why the id of string variable changes. >>> s = 'ab' >>> id(s) 991736112104 >>> s += 'cd' >>> id(s) 991736774080 why the id of string variable changes. The optimization you are trying to trigger is an implementation detail of CPython and is a quite subtle thing: there are many details (e.f. one you are experiencing) which can be preventing it. For a detailed explanation,

CPython from Java?

白昼怎懂夜的黑 提交于 2019-12-01 08:51:08
I need to call CPython code from Java. What tools/APIs/libraries exist out there to help me do this? Jython is not an option since the Python code is heavily dependent upon numpy . edit 1: The main() function should be Java, not Python (i.e. I need to embed CPython into Java, not vice versa.) edit 2: I should also mention that I'll be passing large numeric arrays between Java and Python and therefore a solution that brings the two into the same process space would be preferable (but not mandatory.) You can take a look at using Jepp to embed CPython into Java. Read documentation here . edit:

CPython: Why does += for strings change the id of string variable

寵の児 提交于 2019-12-01 07:13:39
问题 Cpython optimizes string increment operations,When initializing memory for a string, the program leaves extra expansion space for it,so, when incrementing, the original string is not copied to the new location. my question is why the id of string variable changes. >>> s = 'ab' >>> id(s) 991736112104 >>> s += 'cd' >>> id(s) 991736774080 why the id of string variable changes. 回答1: The optimization you are trying to trigger is an implementation detail of CPython and is a quite subtle thing:

Java调用Python程序方法总结(最全最详细)

时光总嘲笑我的痴心妄想 提交于 2019-11-30 22:22:06
如何使用Java调用Python程序 本文为大家介绍如何java调用python方法,供大家参考。 实际工程项目中可能会用到Java和python两种语言结合进行,这样就会涉及到一个问题,就是怎么用Java程序来调用已经写好的python脚本呢,一共有三种方法可以实现,具体方法分别为大家介绍: 1. 在java类中直接执行python语句 此方法需要引用 org.python包,需要下载Jpython。在这里先介绍一下Jpython。下面引入百科的解释: Jython是一种完整的语言,而不是一个Java翻译器或仅仅是一个Python编译器,它是一个Python语言在Java中的完全实现。Jython也有很多从CPython中继承的模块库。最有趣的事情是Jython不像CPython或其他任何高级语言,它提供了对其实现语言的一切存取。所以Jython不仅给你提供了Python的库,同时也提供了所有的Java类。这使其有一个巨大的资源库。 这里我建议下载最新版本的Jpython,因为可以使用的python函数库会比老版本的多些,目前最新版本为2.7。 下载jar包请点击 Download Jython 2.7.0 - Standalone Jar 下载安装程序请点击 Download Jython 2.7.0 - Installer 如果使用maven依赖添加的话,使用下面的语句

How are variables names stored and mapped internally?

邮差的信 提交于 2019-11-30 18:19:49
I read https://stackoverflow.com/a/19721096/1661745 and it seems that in CPython, variables are simply names that are associated with references. There are several things going on with the statement x=5: an int object with the value of 5 is created (or found if it already exists) the name x is created (or disassociated with the last object 'x' labeled) the reference count to the new (or found) int object is increased by 1 the name x is associated with the object with the value '5' created (or found). However, I'm still not clear with exactly how variables are implemented internally. Namely:

String matching performance: gcc versus CPython

谁都会走 提交于 2019-11-30 18:08:50
Whilst researching performance trade-offs between Python and C++, I've devised a small example, which mostly focusses on a dumb substring matching. Here is the relevant C++: using std::string; std::vector<string> matches; std::copy_if(patterns.cbegin(), patterns.cend(), back_inserter(matches), [&fileContents] (const string &pattern) { return fileContents.find(pattern) != string::npos; } ); The above is built with -O3. And here is Python: def getMatchingPatterns(patterns, text): return filter(text.__contains__, patterns) Both of them take a large-ish set of patterns and input file, and filter

Using SQL Alchemy and pyodbc with IronPython 2.6.1

☆樱花仙子☆ 提交于 2019-11-30 16:40:17
I'm using IronPython and the clr module to retrieve SQL Server information via SMO. I'd like to retrieve/store this data in a SQL Server database using SQL Alchemy, but am having some trouble loading the pyodbc module. Here's the setup: IronPython 2.6.1 (installed at D:\Program Files\IronPython) CPython 2.6.5 (installed at D:\Python26) SQL Alchemy 0.6.1 (installed at D:\Python26\Lib\site-packages\sqlalchemy) pyodbc 2.1.7 (installed at D:\Python26\Lib\site-packages) I have these entries in the IronPython site.py to import CPython standard and third-party libraries: # Add CPython standard libs

Python: GIL context - switching

此生再无相见时 提交于 2019-11-30 13:42:38
So, I generally have a pretty good understanding of how the Global Interpreter Lock (GIL) in Python works. Essentially, while the interpreter is running, one thread holds the GIL for N ticks (where N can be set using sys.setcheckinterval ), at which point the GIL is released and another thread can acquire the GIL. The also happens if one thread begins an I/O operation. What I'm a bit confused about is how this all works with C extension modules. If you have a C extension module that acquires the GIL, and then executes some python code using PyEval_EvalCode , can the interpreter release the GIL